WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On my site I have multiple authors, who can add/edit/delete custom posts and upload (attach) images to those posts. The problem is - when they delete their posts, attachments stay on the server. So, I would like to get rid of all files attached to the post when it gets deleted. Can anyone share and example how it's done?

share|improve this question
up vote 2 down vote accepted

WP does not do this by default since there is no guarantee that attachment isn't still being used by some other post.

Basic logic would be to hook into delete_post, query for child attachments and run wp_delete_attachment() on each.

I did quick search in plugin repository and came up with tiny plugin that does just that (seems unmaintained so test before use) - Post Data Delete Advanced.

share|improve this answer
It works, that's great, thanks. – Kovas Jun 26 '11 at 22:49
@Rarst I too can confirm it works great. Thanks! – henrywright Nov 21 '13 at 22:28
Just to note, following your basic logic, I had success when using before_delete_post rather than delete_post. Perhaps by the time delete_post is run, the post's metadata has been removed (which is needed for wp_delete_attachment to function properly). – henrywright Aug 15 '14 at 16:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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