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!

Leave a Comment