File Delete (extras)
Extends Drupal core's file deletion functionality with additional safeguards and options for immediate deletion and usage override.
file_delete
Install
composer require 'drupal/file_delete:^3.0'
composer require 'drupal/file_delete:^2.0'
Overview
The File Delete module enhances Drupal's built-in file deletion capabilities by providing a more robust and flexible file deletion interface. While Drupal core 10.1+ includes basic file deletion functionality, this module hijacks the core route and provides its own form with additional features.
The module changes files from 'Permanent' status to 'Temporary' status by default, allowing Drupal's cron to clean them up according to the configured temporary file retention period. Additionally, it provides options for immediate deletion and the ability to override usage checks for administrative purposes.
A key safety feature is the built-in protection that prevents deletion of files that are currently in use by other entities, displaying helpful error messages with links to view file usages when deletion is blocked.
Features
- Built-in safeguard that prevents deletion of files with active usage by other entities
- Option to delete files immediately, bypassing Drupal's file cleanup cron process
- Option to force delete files by overriding the built-in usage access check (for advanced users)
- Bulk action plugins for performing mass file deletions from the Files view
- Seamless integration with core's Files administration view (/admin/content/files)
- Clear error messages with links to file usage information when deletion is blocked
- Automatic cleanup of file references from the 'file' module's usage tracking
Use Cases
Cleaning up unused files after content migration
After migrating content to a new Drupal site, you may have orphaned files that are no longer referenced by any content. Use the Files view (/admin/content/files) to identify files with zero usage, select multiple files, and use the 'Mark file for deletion' bulk action to safely queue them for removal during the next cron run.
Immediately removing sensitive files
If a file containing sensitive information was accidentally uploaded, administrators with the 'Instantly Delete files' permission can immediately remove it from the server without waiting for cron. Navigate to the file's delete form and enable the 'Do you want to delete the file immediately?' option.
Managing disk space on high-traffic sites
On sites with heavy file uploads, temporary files can accumulate. Use the bulk 'Immediately delete (with usage checks)' action to instantly free up disk space while still maintaining the safety check that prevents deletion of files in active use.
Resolving file reference issues with Media entities
When working with Drupal Media, files are often referenced by Media entities. Before deleting a file, you must first delete the associated Media entity. The module will show you which modules are using the file and provide a link to view the usages, helping you identify and remove Media references before file deletion.
Force deleting files during site maintenance
During major site restructuring, administrators with the 'Override Usage Delete files' permission can force delete files even when usage tracking indicates they are in use. This is useful when you know the usage tracking is incorrect or when you intentionally want to remove files regardless of consequences.
Tips
- Always check file usages before deleting to avoid broken content - the module helps by blocking deletion of used files
- Use 'Mark file for deletion' for regular cleanup tasks, as it follows Drupal's standard file lifecycle
- Reserve 'Immediately delete' for urgent situations or when you need to free disk space quickly
- When working with Media entities, remember to delete the Media entity first, then the underlying file
- Grant 'Override Usage Delete files' permission sparingly, as it can cause broken media and links if misused
- Configure an appropriate temporary file retention period in File System settings based on your site's needs
Technical Details
Admin Pages 1
/file/{file}/delete
Confirmation form for deleting a specific file. Displays the file name and URI, provides options for immediate deletion and force deletion (if user has appropriate permissions), and prevents deletion of files that are in use unless force delete is enabled.
Permissions 2
Hooks 2
hook_entity_type_build
Alters the file entity type definition to replace the default delete form class with FileDeleteForm
hook_help
Provides help text for the module's help page
Troubleshooting 4
Drupal keeps temporary files for a configurable period (default 6 hours) before deletion. Check the retention setting at Administration > Configuration > Media > File System. Alternatively, use the 'Immediately delete' option or action to bypass this waiting period.
The module prevents deletion of files with active usage to avoid broken content. Click the provided link to view file usages, then remove the references from the using entities (e.g., delete Media entities or edit content) before attempting to delete the file again. Administrators can also enable 'force delete' to override this protection.
After installing the module, you need to manually add the field to your Files view. Go to Administration > Structure > Views > Files > Edit, add a new field, and search for 'Link to delete File'.
Ensure the view has a bulk operations field configured. The module installs action configurations automatically, but the view must be configured to display bulk operations. Check that system.action.mark_file_for_deletion and system.action.immediate_delete configurations exist.
Security Notes 5
- Both module-specific permissions are marked with 'restrict access: true', indicating they should only be granted to trusted administrators
- The 'Override Usage Delete files' permission allows bypassing safety checks and can result in broken content if misused
- The 'Immediately delete' permission bypasses Drupal's cron-based cleanup, providing no recovery window
- Direct database manipulation is used in ImmediateDeleteWithUsageChecks action - ensure proper database backups are maintained
- File deletion is permanent and cannot be undone - consider implementing a backup strategy for critical files