Don’t forget to subscribe to our YouTube channel to stay up-to-date.
File Management Series
Removing files from Drupal is more tricky than you might think. There has been another tutorial about this using the module File Delete. It requires going through a cycle of minimum 6 hours. By default, Drupal protects files from removal which are still being used and referenced in other content. Instead of directly deleting a file, the linked usages need to be cleared first, and the files marked as temporary, then finally the system will remove them during cleanup.
This is actually a good policy. However, sometimes it’s annoying to wait for a few hours before these files are being cleaned up. In particular, if we are sure these files are unused or orphaned, there is another module called Fancy File Delete, which solves this problem.
Fancy File Delete allows you to delete files straight away without having to wait.
However, great care must be taken when using this module, because it has a ‘force delete’ option. Use of this module should be restricted to experienced administrators.
Before we begin, go download and install Fancy File Delete.
Using the following Drush commands:
composer require drupal/fancy_file_delete
NOTE: The module requires the Views Bulk Operations (VBO) module which will be downloaded when you run the above command.
To follow a proper procedure to delete files, administrators should still go through the proper file handling process by removing them from the nodes and the media library first, even though these processes can be bypassed using this module.
After installing and enabling the modules, go to Admin >> Configuration >> Content authoring >> Fancy File Delete. In the first tab ‘Info’, it shows a brief description on how to use the module.
Enter the ‘List’ tab, a list of all the files in the system can be found from here. Select the file(s) to be deleted using the checkbox in front of each file, then select ‘Delete Files’ in the ‘Action’ option on the top of the page, then click the ‘Apply to selected items’ button.
Note that there is another option for Action, which is ‘FORCE Delete Files (No Turning Back!)’. When a file cannot be deleted, try this option but handle with care.
Can’t See Action Drop-Down
If you can’t see the Action drop-down on the list page that means you’ll need to reconfigure the view.
1. Edit the List view by clicking on the pencil icon.
2. Once you’re editing the view, click on the “Views bulk operations” field.
3. Enable the actions by clicking “Select all”.
Then save the view and you should see the Action drop-down.
Delete Files manually by FID
As can be seen above, there is a File ID for each file. Files can be deleted using this FID by entering them under the ‘Manual’ tab. Just enter the FIDs of the files to be deleted, one per line, and press the ‘Engage’ button.
Deleting Orphaned and Unmanaged Files
Additional options are available to delete orphaned and unmanaged files if the system detects them.
Deleting Files from the ‘Files’ or ‘Image’ Fields
Note that in the above procedure using ‘Fancy File Delete’ module, it is not a requirement to remove them from the nodes first:
- It bypasses the default file delete procedure in Drupal, and therefore it should be handled with care.
- Files deleted will also be removed from previous node revisions too.
- Files deleted will also be removed from the list of files under Admin >> Content >> Files
- Files deleted are permanently removed totally from the system, including the physical file in the server.
- It does not require waiting for the minimum 6 hours requirement.
Deleting Files from the ‘Media’ Field
When the files are uploaded through the media module and deleted without first removing them from the nodes and the media library first, this ‘Fancy File Delete’ module still remove the files same as above, except the following:
- Files deleted will still be removed both physically from the server and from the list of files under Admin >> Content >> Files, like above.
- The file will not be shown in node because it is empty, but an empty entity will remain both in the node and in the media library.
If you use Drush, you can delete files straight away using the “drush entity:delete” command.
drush entity:delete file 1
This command will remove the file from the Files page and delete it from the file system and you won’t have to wait.
This module offers a more straightforward and efficient way to remove files from a Drupal site permanently. It is powerful and capable of bypassing the default file handling procedures in Drupal. But that also means bypassing the file protection mechanism of Drupal. It handles file deletion in an efficient manner which is an advantage, but it is also a disadvantage at the same time.
On a large site where there are more users and content authors. When deleting files are granted to more people, it is difficult to guarantee that everybody is careful enough to understand the underlying structure and relationships between files and content. There is a higher risk of accidental deletion of files. In such situations, a better protection mechanism might be necessary.
On a small site or in a small team where content is easier to manage and control, efficiency might be preferred.