5

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?

3

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.

| improve this answer | |
  • 4
    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
  • Same here... after running some tests I've noticed when you delete the post 'post_parent' data gets removed from all related attachments. – Luis Rivera Sep 28 '19 at 17:05
8

I wrote the following function that uses the logic suggested by @rarst in his answer.

add_action( 'before_delete_post', function( $id ) {
  $attachments = get_attached_media( '', $id );
  foreach ($attachments as $attachment) {
    wp_delete_attachment( $attachment->ID, 'true' );
  }
} );

IMPORTANT: Keep in mind it will only be called when the posts are permanently deleted from the trash! If you want to change this behaviour, include the following line in your wp-config.php file:

define('EMPTY_TRASH_DAYS', 0);

If this constant is set to 0, the trash functionality will be disabled and the 'Delete Permanently' button will appear instead of 'Trash' button. If you click 'Delete Permanently' button, the item will immediately be deleted without any alert message.

(From the Codex)

| improve this answer | |
  • 1
    Worked for me - Added a check to only apply this on a specific post type, and everything worked like a charm. – Stender Dec 6 '17 at 10:08
  • Doesn't work in my case, the post is deleted and the attachment is deleted from media library, but the file with it's other sizes still exist in the folder. – Sam Tyurenkov Apr 9 at 22:47
2

Expanding on other answers here for use with only a specific post type.

add_action( 'before_delete_post', 'delete_all_attached_media' );

function delete_all_attached_media( $post_id ) {

  if( get_post_type($post_id) == "post" ) {
    $attachments = get_attached_media( '', $post_id );

    foreach ($attachments as $attachment) {
      wp_delete_attachment( $attachment->ID, 'true' );
    }
  }

}

This will delete all attached media when a post is permanently deleted.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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