Is there a way to list all unused files which reside inside public file system and inside a theme directory and list them, or maybe even have an option to delete them automatically?

I mean files which are currently not linked from any css stylesheet or from any node.

  • I'd like to know the answer to that too, thanks for posting the question!
    – NPC
    Commented Feb 1, 2012 at 8:35
  • Are you referring to files uploaded with a Field (like ImageField) or files in general (uploaded via IMCE)? I don't think you can track IMCE uploads without searching each node body for references.
    – Chaulky
    Commented Feb 10, 2012 at 18:07
  • Yes, I am uploading via IMCE. I was hoping there is a module which does what you said: scan nodes to find image references, then for internal drupal references (without domain) scan relevant directories and compare both to find unused files. Perhaps something similar is done with links in an existing module to find broken links. So I thought maybe similar module exists for images but maybe not..
    – camcam
    Commented Feb 12, 2012 at 7:43

7 Answers 7


You can find orphaned files by running the following MySQL 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 returns all files which have no associated node. I'm not sure if it's safe to delete the returned rows and files, probably also depends on your module setup. Only use at your own risk!

Source: http://drupal.org/node/733258#comment-5582764

  • 5
    You could have shared the link to the ORIGINAL post you COPIED your post from....... drupal.org/node/733258#comment-5582764 I think it's very unethical not to show the source.
    – Sk8erPeter
    Commented Aug 21, 2012 at 13:00
  • Following the link you have posted above, I also think the code in drupal.org/node/733258#comment-7427898 is useful because it will remove both the orphaned files and their corresponding entries in the database. Commented Sep 24, 2013 at 14:20
  • Actually, @Sk8erPeter, it's proper to sum up information in the answers and then link to it. Especially if they come from a source outside of Stack Exchange.
    – Christia
    Commented Mar 24, 2017 at 19:01
  • 1
    @Christia, if you read the original post before I edited it (I put the link into the answer), you can see that David has copied someone else's comment word by word without even mentioning its source and putting a quote sign around the post. This can be considered as plagiarism, this is what my comment was about. :)
    – Sk8erPeter
    Commented Mar 25, 2017 at 0:14
  • You'll find this produces some duplicate file ids, to avoid that and add a count of the duplicates I added a group by. SELECT fm.*, COUNT(*) 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 GROUP BY fm.fid;
    – Cameron
    Commented Jul 17, 2017 at 0:46

For those coming to this post three years later, there's a little module you can use to do this called Fancy File Delete.

At the time of this post, it's in beta, so use it at your own risk. As always, cleaning up orphaned anything through DB queries can be sketchy and its success depends heavily on your particular module setup.

  • I found this module to be very buggy - to the point where it was useless. ymmv.
    – Felix Eve
    Commented May 15, 2017 at 3:14

Something that may help to identify "files that are no longer attached to nodes or files and directory that are not in the file managed table" (as in the duplicate question about "How to do delete unused files?"), is to use the File Checker module. Some details about it, from its project page:

In a perfect Drupal world your server filesystem and its correspoding entries in Drupal's files table are 100% synchronized. But what if parts of your file system have been corrupted due to some disk failure? Or one of your modules messed up your database and files? Or your deploy script went beserk? Well, then this module will help you to monitor and find out which files are out of sync.

Out of the box the files table has two kind of statuses: Temporary (0) and Permanent (1). The file checker introduces an additional status Missing (2). In scope of the verification process which can be triggered in various ways the status column of the files table is updated.


  • Run verification process: on demand, via cron, via drush (in planning)
  • File list overview page with filters
  • Views integration
  • Drush command for file checking

If you want to export the results of a view it's recommended to use views_data_export module.

So what you could do is like so:

  • Clone (copy) your site to some dev environment, but don't copy any of the files in the directory you want to check. As a variation (if this question is about a non-production status site), just temporary move all files out of that directory.
  • Use the File Checker module to find out what files are "missing": these are the files that are obviously not unused. But any file that this module does not argue about is ... unused!
  • By copying all missing files to the correct location of the directory you want to check, you then step-by-step recreate a perfect content of your directory.

Note: even though this question is about D7, the is an (alfa) version of it also for D8.

  • Very helpful approach to solve my issue
    – kb8
    Commented May 26, 2016 at 13:12

there is one module that removes unwanted files fancy file delete.

View of all managed files with an option to force delete them via VBO custom actions Manually deleting managed files by FID (and an option to force the delete if you really want to). Deleting unused files from the default files directory that are not in the file managed table. AKA deleting all the unmanaged files. Deleting unused files from the whole install that are no longer attached to nodes & the file usage table. AKA deleting all the orphaned files.

  • 1
    I have a bad experience with large massive operations and vbo module. Initially I will use a approach like suggested by Pierre.Vriens, after I will use your suggest module to maintain clean the files directory.
    – kb8
    Commented May 26, 2016 at 13:10

You can delete unused file by

  1. Delete manually from the file_managed table, by some query like

    $this->database ->delete('file_managed') ->condition('fid', $fid, '=') ->execute();

  2. Setting the status of a file 0, to mark is as temporary file, so the cron will delete it after certain time.
    $file = File::load ($fid); $file->setTemporary();


The fancy file delete module did not work at all for me. Here is a more manual alternative.

To delete files from a folder that are not in the managed file table you can:

1) Create a list of all the managed files:

mysql whateverdb -e "select filename from file_managed" > ~/managed-files.txt

2) Delete all files from a directory that are not in that list. To do this I used a small bash script:



for FILE in $IMG_FOLDER/*; do
  if ! grep "$FILE" "$EXCLUDES"; then
        echo "Deleting $FILE"
        rm -f "$FILE"

Just change the IMG_FOLDER variable to the path of whatever folder you want to delete files from (and update the path to your excludes file)


There is this module as well that is an alternative to the "fancy delete" https://www.drupal.org/project/unmanaged_files

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.