I'm using Drupal 7. On my site, users can add pictures via a content type, Pictures, that makes use of the core image field.

If a user uploads an image file and saves the node, then if the node is deleted the image file will be deleted as well.

However, if a user uploads an image file and then leaves the page, the image file remains stored in the filesystem and is not deleted. This means that if users are careless, it is possible for them to add lots of pictures that will never be seen and never be deleted, wasting lots of disk space.

Is there any way to automatically delete these "orphan" files?

1 Answer 1


That would be difficult to accomplish automatically, since as you pointed out, these nodes / profiles are not saved and therefore the data isn't loaded into Drupal 7's file management table. Drupal 7 will automatically delete all files it is tracking in the file management table when the node is deleted.

Another way to approach this then is to try and detect any files in your target folder (ex. 'files/') that do not have a corresponding data entry in the database and then delete those. Here is a script from Caribbean New Media that does just this task (and even cleans up image styles):

The script will scan through your chosen directory (and subdirectories), for every file found, it will endeavour to cross-reference that file within your chosen table. If the file doesn't exist in the tables, it will delete the original file and all it's generated image style files you have defined.

Definitely should be carefully configured and tested in a safe development environment before moving to your production site, but once running correctly, you can set this up in a cron job to at least have it run every few days / week to clean up orphaned uploads.

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.