File Delete (extras)
Extends Drupal core's file deletion functionality with additional safeguards and options for immediate deletion and usage override.
file_delete
インストール
composer require 'drupal/file_delete:^3.0'
composer require 'drupal/file_delete:^2.0'
概要
File Deleteモジュールは、より堅牢で柔軟なファイル削除インターフェースを提供することで、Drupalの組み込みファイル削除機能を強化します。Drupal Core 10.1以降には基本的なファイル削除機能が含まれていますが、このモジュールはCoreのルートを乗っ取り、追加機能を備えた独自のフォームを提供します。
このモジュールはデフォルトでファイルのステータスを「Permanent(永続)」から「Temporary(一時)」に変更し、設定された一時ファイル保持期間に従ってDrupalのCronがクリーンアップできるようにします。さらに、即時削除のオプションや、管理目的で使用状況チェックを上書きする機能も提供します。
主要な安全機能として、他のエンティティで現在使用中のファイルの削除を防止する組み込みの保護機能があり、削除がブロックされた場合にはファイルの使用状況を確認するためのリンク付きのエラーメッセージを表示します。
Features
- 他のエンティティでアクティブに使用されているファイルの削除を防止する組み込みの安全機能
- DrupalのファイルクリーンアップCronプロセスをバイパスして、ファイルを即時削除するオプション
- 組み込みの使用状況アクセスチェックを上書きしてファイルを強制削除するオプション(上級ユーザー向け)
- Filesビューから一括ファイル削除を実行するためのバルクアクションプラグイン
- Coreのファイル管理ビュー(/admin/content/files)とのシームレスな統合
- 削除がブロックされた場合に、ファイル使用状況情報へのリンク付きの明確なエラーメッセージを表示
- 「file」モジュールの使用状況追跡からファイル参照を自動クリーンアップ
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.
権限 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