Delete Images from Media Gallery when Post moved to Trash

I have a client who sells unique products on their custom WordPress site. The products listed have limited quantities and once they’re sold they won’t be available again.

From the front end, it’s easy enough to just “Trash” a post (product) and they won’t display. However, this could quickly lead to unnecessarily large storage on the server since WordPress doesn’t delete post attachments (images) when posts are trashed.

Here’s a code block which will delete images when posts are moved to the trash.

This could be included in functions.php or wrapped inside a custom plugin, it’ll work the same either way. Be sure to read the *disclaimer at the bottom before breaking things.

Deleting the featured image only:

This code will delete just the featured image when a post is moved to trash:

add_action( 'trashed_post', 'mtp_delete_attached_thumbnail_for_trashed_product', 20, 1 );

function mtp_delete_attached_thumbnail_for_trashed_product( $post_id ) {
 
 // gets ID of post being trashed
 $post_type = get_post_type( $post_id );
 
 // does not run on other post types
 if ( $post_type != 'post' ) {
 return true;
 }
 
 // get ID of featured image
 $post_thumbnail_id = get_post_thumbnail_id( $post_id );

 // delete featured image
 wp_delete_attachment( $post_thumbnail_id, true );
}

Delete images from a custom field:

In many cases you’ll have more images associated with a post or product. This code will also delete gallery images in addition to the featured image.

add_action( 'trashed_post', 'mtp_delete_attached_thumbnail_for_trashed_product', 20, 1 );

function mtp_delete_attached_thumbnail_for_trashed_product( $post_id ) {
 
 // gets ID of post being trashed
 $post_type = get_post_type( $post_id );
 
 // does not run on other post types { $post }
 if ( $post_type != 'post' ) {
 return true;
 }
 
 // get ID of featured image
 $post_thumbnail_id = get_post_thumbnail_id( $post_id );

 // gets array from custom field { $gallery } 
 $gallery_images = get_field('gallery', $post_id);

 // loop through { $gallery } 
 foreach ($gallery_images as $gallery_image) {
 
 // get each attachment ID
 $gallery_id = $gallery_image['id'];

 // delete attachments
 wp_delete_attachment( $gallery_id, true );
 }
 
 // delete featured image
 wp_delete_attachment( $post_thumbnail_id, true );
}

Dealing with custom post types:

For both examples, if you need this to work on a custom post type, i.e. “product”. You’ll need to replace this:

if ( $post_type != 'post' ) {

with this:

if ( $post_type != 'YOUR_POST_TYPE_NAME' ) {

If you have a gallery associated with the post, you’ll also need to change the gallery title reference. In my case, I’m using an ACF gallery named “Gallery”. So you’d update this line:

$gallery_images = get_field('gallery', $post_id);

with:

$gallery_images = get_field('YOUR_GALLERY_FIELD_NAME', $post_id);

Tested on 5.6 – Originally coded on WP 4.7.5

*This will permanently delete your media so as always, be sure to run a backup of your site files and database before testing on your site!

Find this useful? Let me know!

6 responses to “Delete Images from Media Gallery when Post moved to Trash”

  1. Chad Avatar

    Michael, thank you for this. I have a website that utilizes Listify and listings are always being deleted and added. Do you know if this still works on WordPress 5.2? Thanks in advance.

  2. Wayne Avatar
    Wayne

    Great snippet. Save me hundreds of hours.

    I am breaking a large site into smaller sub-sites and the client was sloppy categorising the images. I was dreading searching and deleting individual images.

  3. Thibault Avatar
    Thibault

    Hi Michael, thats a pretty nice post I was looking for some time ago. Now we are wiling to implement it for posts and have two questions.

    – would your CPT work for Woo products ?

    – Did you find a trick to check if images are used elsewhere on the website before deleting them ? This much needed to make it a perfectly reliable solution.

    Regards.

  4. Ken Avatar
    Ken

    Thanks for this.

  5. Nick Avatar
    Nick

    I just tried this with my site but sadly it didn’t work. changed ther post type to product but it still left the images behind. any clues on if anything would have changed? i added the code to the child theme functions.php file

  6. Jan Avatar
    Jan

    Hello Michael,
    I am using this code and it works fine. Can you help me with another costom field (PDF Document) using WordPress Custom post type. I want to delete the PDF in the media folder when the post is moved to trash. Please get in contact, I am from Germany, Jan

Leave a Reply

Your email address will not be published. Required fields are marked *