Skip to content

BUG: Orphan thumbnail files permanently leak disk space when folders are deleted #1159

@Nakshatra480

Description

@Nakshatra480

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

When a user deletes a folder through Settings → Folder Management, the thumbnail image files on disk are never cleaned up. The SQL ON DELETE CASCADE removes the image records from the database, but the physical thumbnail_*.jpg files in the app's thumbnail directory remain on disk permanently. This causes a silent disk space leak that grows every time a folder is removed.

Expected Behavior

When a folder is deleted, all associated thumbnail files on disk should also be removed. The delete_folders route should query all thumbnail paths for images belonging to the folders being deleted before removing the DB records, and then delete those files from disk.

Root Cause

The delete_folders route in backend/app/routes/folders.py only calls the database batch delete function which performs a SQL DELETE — it never touches the filesystem to remove the corresponding thumbnail files.

Interestingly, the sync/obsolete flow in backend/app/utils/images.py (image_util_remove_obsolete_images) does correctly clean up thumbnails when images are found to be missing from the filesystem — but this function is never called during the folder deletion flow.

The same problem also exists in folder_util_delete_obsolete_folders in backend/app/utils/folders.py — it deletes folder records from the database without any thumbnail cleanup.

Steps to Reproduce

  1. Open PictoPy and add a folder containing images (e.g., 50 photos).
  2. Wait for processing to complete (thumbnails generated).
  3. Note the files in the thumbnails directory (~/.local/share/PictoPy/thumbnails/).
  4. Go to Settings → Folder Management and delete the folder.
  5. Check the thumbnails directory again — all thumbnail files from step 3 are still present.
  6. Repeat steps 1–5 — orphan files keep accumulating.

Notes

Files Involved

  • backend/app/routes/folders.pydelete_folders() (primary fix needed)
  • backend/app/utils/folders.pyfolder_util_delete_obsolete_folders() (also needs fix)
  • backend/app/database/images.pydb_get_images_by_folder_ids() (needed to query thumbnails before deletion)
  • backend/app/utils/images.pyimage_util_remove_obsolete_images() (reference for correct thumbnail cleanup)

Record

  • I agree to follow this project's Code of Conduct

Record

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions