When a node is updated by replacing a new file upload, old files exist on the server maintaining versions.

I want to delete old files from the server when content is updated.

1 Answer 1


This is already discussed topic. I found some source.

Drupal Forum and Original Source

After that I would create a cron job (Implementing hook_cron). And insert this snippet.

I hope it works out for you.

Alternatively, if you dont want this action to happen once per day, you can insert it in hook_submit, or hook_update. But I would go for the first method.

Do it once per day/week/whatever time period you like.

  • Thanks for the solution @Oleg Videnov, I put the code snippet in hook_cron in my custom module.It deletes all the file that are not attached to any node.But next thing that has to be done is file name is getting changed like it appends filename_0.pdf, I want the same name e.g. filename.pdf Oct 22, 2013 at 9:47
  • Can you try to flush your cache before this action ? Oct 22, 2013 at 10:56
  • No result. @Oleg Videnov Oct 22, 2013 at 13:00

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.