I want to remove/delete files from site directories (e.g. /sites/default/files/*) that are not used in site. There are thousands of files that causing fillup size. Any suggestions for this?


5 Answers 5


I found this post https://drupal.org/node/733258... may help...

The main purpose talked in this issue is about removing the images which doesn't have any use directly from database via SQL queries.

Audit files module is suggested as a good start (because doesn't delete files) to find files which are unused.

This code snippet is one of the suggested solution from the issue :

//db_query to find all files not attached to a node:
$result = db_query("SELECT fid FROM file_managed WHERE NOT EXISTS (SELECT * FROM file_usage WHERE file_managed.fid = file_usage.fid) ");

//Delete file & database entry
for ($i = 1; $i <= $result->rowCount(); $i++) {
  $record = $result->fetchObject();
  $file = file_load($record->fid);
  if ($file != NULL) {
  } }

A simple way of searching the unused files is running the following query:

FROM file_managed AS fm
LEFT OUTER JOIN file_usage AS fu ON (fm.fid = fu.fid)
LEFT OUTER JOIN node AS n ON (fu.id = n.nid)
WHERE fu.type = 'node' AND n.nid IS NULL

This query would return all the files that are not associated with any node.

Also, in order to avoid landing up in such situations its better to add a file_usage_add() to every file. This will enable the system to know that the file is being used. Appending a file_usage_delete() does a cron type job of removing unused files from the system.

  • This query will not return any unmanaged files.
    – Mołot
    Jun 7, 2013 at 8:40
  • The query seems identical to the one here drupal.org/node/733258#comment-5582764 that was posted 4 months earlier in the post which @jramby linked to. If you look at that thread, there are some follow up comments which could be useful.
    – AdamS
    Nov 20, 2015 at 10:12
  • The file_usage table has a 'type' column that can be for example 'taxonomy_term' (perhaps an image field was added to a taxonomy) or 'file' (for IMCE). This query will not scan these other types.
    – AdamS
    Nov 20, 2015 at 10:15

There is a sandbox project called 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.

  • The drush command 'file-delete-unused' could not be found. Run [error] drush cache-clear drush to clear the commandfile cache if you have installed new extensions.
    – Yuseferi
    Mar 2, 2016 at 20:41

You could either remove it manually or could go through https://drupal.org/node/733258 which would help you remove unwanted images.


Here is my example of removing unused gmap_markers files using drush:

drush sqlq "SELECT fid FROM file_managed WHERE status = 1 AND filename LIKE 'gmap_markers%'" | xargs -L1 -I% drush -v eval "file_delete(file_load(%));"

You can see all the Drupal files by:

drush sqlq "SELECT fid, filename, uri FROM file_managed WHERE status = 1"

To see if any specific file is not used, I'd suggest to scan for each of these files in the filesystem and database by the following example commands:

grep -R "my_file.jpg" /Drupal/webroot
drush --ordered-dump sql-dump | grep "my_file.jpg"

Once the file doesn't exists anywhere else apart of standard file_managed, etc. then I think it can be removed safely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.