Delete post with all files attached to it

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?

4 s
4

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.

Leave a Comment