Last updated 2 March 2015. Created on 2 March 2015.
Edited by bneil. Log in to edit this page.

Depending upon how you manage media on your site, media and file entity address file cleanup in different ways. Watch #1399846: Make unused file 'cleanup' configurable for a core issue related to this.

Files added through file/add form

If a file is uploaded to a site via the upload form at file/add it will be marked as permanent and will never be deleted via core's file cleanup functionality. Even if you use a file that had been initially uploaded this way on a file field on a node and delete the node, the file will remain permanent in the file system.

However, you can use navigate to admin/content/file, select the file and in the Update Options select “Indicate that the selected files are temporary and should be removed during cron runs."

Media library - Media WYSIWYG module

If a file is uploaded via the media library button in the WYSIWYG, it will also be permanent, like above. Even if you delete the node, the file will remain in the Media library.

However, any file embedded via the media library button will record usage when you look at the usage tab of the file.

File field

If a file is uploaded via a file field on a content type and the node is deleted, if the file is not used anywhere else on the site, it is deleted immediately.

Other considerations

If you upload a file in the first step of the Add file form and realize that it’s not the right file and click the remove button, it is removed from the file system immediately.

If you upload a file and do not finish through creating the file “entity” by clicking save at the end of the multistep form, the file is marked as temporary, and it gets deleted on a cron run if the file is older than six hours.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

osopolar’s picture

If you are looking for a drush command, please check out the sandbox module File Delete, which provides the drush command drush file-delete-unused --all to delete all files reported in file_managed which aren't in file_usage. It deletes the file itself and the database-entry.

dbourrion’s picture

Hi osopolar
After putting your sandbox in my /sites/all/modules, I have

bu@bu-test:/var/www/html/labua/sites/all/modules$ drush file-delete-unused
Command file-delete-unused needs the following module(s) enabled to run: file_delete. [error]
The drush command 'file-delete-unused' could not be executed.

but

bu@bu-test:/var/www/html/labua/sites/all/modules$ drush en file_delete
file_delete was not found. [warning]
No release history was found for the requested project (file_delete). [warning]
There were no extensions that could be enabled. [ok]

What am I missing ?