cleanup_orphaned_photos command removes photo files that are no longer referenced in the database, freeing up storage space. This is useful after deleting items or during development when photo uploads fail.
How it works
The command scans photo directories and identifies files that:- Are not referenced in the
collection_phototable - Are from incomplete form submissions (photos uploaded but item never created)
- Are older than a specified threshold
Basic usage
Arguments and options
Preview mode - show what would be deleted without actually deleting files.Example:
Show detailed output including each file being processed.Example:
Only clean up photos from incomplete form submissions (uploaded but never attached to an item).Example:
Only clean up photos older than specified hours (used with
--incomplete-only).Example:Cleanup modes
All orphaned photos
Removes all photo files not referenced in the database:- Photos from deleted items
- Manually deleted files that still exist on disk
- Any file in
item_photos/oritem_photos_avif/without a database reference
Incomplete submissions only
Removes photos from failed form submissions (safer option):- Photos uploaded during item creation but never attached to an item
- Photo records with
content_type_id IS NULL - Only photos older than the specified threshold (default: 24 hours)
Example workflows
Safe cleanup (recommended)
Aggressive cleanup
Remove all incomplete photos older than 1 week:Production deployment
Run weekly via cron or Celery Beat:Output example
Automatic cleanup
FootyCollect includes automatic cleanup via Celery Beat periodic tasks:- Incomplete photos - Every 6 hours (photos from failed submissions)
- All orphaned photos - Every 7 days (comprehensive cleanup)
Storage directories
The command scans these directories:MEDIA_ROOT/item_photos/- Original uploaded photosMEDIA_ROOT/item_photos_avif/- AVIF-optimized versions
Database cleanup
When using--incomplete-only, the command also deletes orphaned database records:
Troubleshooting
Permission denied
Permission denied
Ensure the user running the command has write permissions on the media directory:
Files still present after cleanup
Files still present after cleanup
Check if files are in use by another process or if the storage backend is remote (S3/R2).For remote storage, files are not automatically deleted from the bucket. Use the cloud provider’s lifecycle policies instead.
Database records without files
Database records without files
If photos are referenced in the database but files are missing, use:Then investigate missing files in logs.