feat: delete orphaned files when their post is deleted#468
Draft
feat: delete orphaned files when their post is deleted#468
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fof_upload_file_postspivot rows are cascade-deleted by the DB, but the actualfof_upload_filesrecords and physical files on storage remained orphaned indefinitelyModelObserveronPostthat hooks in before deletion (while pivot rows still exist) to optionally clean up exclusively-associated filesdeleteFilesOnPostDelete(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 untouchedphp flarum fof:upload --cleanup) continues to handle orphaned files as beforeChanges
src/Listeners/CleanUpFilesOnPostDelete.php— new model observerextend.php—ModelObserverextender + settings defaultjs/src/admin/components/UploadPage.tsx— admin toggleresources/locale/en.yml— locale keysTest plan
shared = true): delete post → file survivesphp flarum fof:upload --cleanupremoves them🤖 Generated with Claude Code