How to remove images associated with the deletion of a product, etc

Don’t you sometimes think that in WordPress the media library goes on its own way?

I mean, it’s funny that when you upload an image to illustrate a product, page, or entry, it appears in the media library as “attached” to that content, but if for some reason you decide to delete the product, the images are still there, even though you don’t need them anymore.

This is especially worrying in the case of online stores, where images are associated with products and most of the time are not reusable.

Well, congratulations, because here’s a way to make sure that when you delete a product or whatever, the attached images are automatically deleted.

This is how to delete the featured image by deleting the content

With the following code, only the featured image will be deleted when the product is deleted, etc.

/* Delete featured image when you delete the post */
add_action( 'trashed_post', 'wphelp_delete_featured_image_with_product', 20, 1 );
function wphelp_delete_featured_image_with_product( $post_id ) {
 
 // Get the ID of the type of content sent to the trash
 $post_type = get_post_type( $post_id );
 
 // Do not run on other content types
 if ( $post_type != 'product' ) {
 return true;
 }
 
 // Get the ID of the featured image
 $post_thumbnail_id = get_post_thumbnail_id( $post_id );
 
 // Delete featured image
 wp_delete_attachment( $post_thumbnail_id, true );
}Code language: PHP (php)

And this is how to delete the image gallery and the featured image when deleting the content

If you also want to delete the additional images attached to the content then the code would be this one:

/* Delete the image gallery and the featured image when you delete the post */
add_action( 'trashed_post', 'wphelp_delete_featured_image_gallery_with_product', 20, 1 );
 
function wphelp_delete_featured_image_gallery_with_product( $post_id ) {
 
 // Get the ID of the type of content sent to the trash
 $post_type = get_post_type( $post_id );
 
 // Do not run on other content types
 if ( $post_type != 'product' ) {
 return true;
 }
 
 // Get the ID of the featured image
 $post_thumbnail_id = get_post_thumbnail_id( $post_id );
 
 // Get the custom field array from the gallery 
 $gallery_images = get_field('gallery', $post_id);
 
 // gallery loop 
 foreach ($gallery_images as $gallery_image) {
 
 // Obtain the ID of each attachment
 $gallery_id = $gallery_image['id'];
 
 // Delete Attachments
 wp_delete_attachment( $gallery_id, true );
 }
 
 // Delete featured image
 wp_delete_attachment( $post_thumbnail_id, true );
}Code language: PHP (php)

How to use and customize this codes

As usual, if you want to use one of these codes you must add it to your customization plugin or, failing that, in the functions.php file of the active theme (at the end of it).

In the examples the type of content chosen is a product, but you can customize it to act on other types of content, such as posts, pages, etc.

Similarly, with image galleries, the custom field is the standard for WordPress galleries (gallery) but if it is another one you use, through a plugin for example, just replace it.

The final goal is that when you send a product, entry, page or whatever to the trash, the images associated with it will accompany it.

Read this post in Spanish: Cómo eliminar las imágenes al borrar un producto o contenido

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

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

Scroll to Top
Skip to content