Skip to content

feat: delete orphaned files when their post is deleted#468

Draft
imorland wants to merge 2 commits into2.xfrom
im/fix-post-delete-cascade
Draft

feat: delete orphaned files when their post is deleted#468
imorland wants to merge 2 commits into2.xfrom
im/fix-post-delete-cascade

Conversation

@imorland
Copy link
Copy Markdown
Member

Summary

  • When a post is deleted, fof_upload_file_posts pivot rows are cascade-deleted by the DB, but the actual fof_upload_files records and physical files on storage remained orphaned indefinitely
  • Adds a ModelObserver on Post that hooks in before deletion (while pivot rows still exist) to optionally clean up exclusively-associated files
  • New admin setting deleteFilesOnPostDelete (off by default) — when enabled, files used only in the deleted post are removed from storage and the DB immediately; files referenced by multiple posts are left untouched
  • When the setting is off, the existing manual cleanup command (php flarum fof:upload --cleanup) continues to handle orphaned files as before

Changes

  • src/Listeners/CleanUpFilesOnPostDelete.php — new model observer
  • extend.phpModelObserver extender + settings default
  • js/src/admin/components/UploadPage.tsx — admin toggle
  • resources/locale/en.yml — locale keys

Test plan

  • Enable the setting; upload a file in a post; delete the post → file gone from DB and storage
  • File referenced in two posts: delete one → file survives, only pivot row removed
  • Shared file (shared = true): delete post → file survives
  • Setting off: delete post → files remain; php flarum fof:upload --cleanup removes them
  • Storage adapter delete failure → DB record preserved (no silent data loss)

🤖 Generated with Claude Code

imorland and others added 2 commits February 22, 2026 21:46
When a post is deleted the fof_upload_file_posts pivot rows are removed
by DB cascade, but the fof_upload_files records and physical files on
storage remained orphaned indefinitely.

- Add a ModelObserver on Post that fires its `deleting()` hook *before*
  deletion, while pivot rows still exist, so file associations can be
  read and acted on
- New setting `fof-upload.deleteFilesOnPostDelete` (default: off) — when
  enabled, files exclusively linked to the deleted post are immediately
  removed from storage and the DB; files shared across multiple posts are
  left untouched
- Admin toggle added to the settings page alongside the existing
  hotlink/download-logging controls
- When the setting is off the existing manual cleanup command
  (`php flarum fof:upload --cleanup`) continues to handle orphaned files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants