Even though WooCommerce is a very robust shopping platform and has developed a lot in recent years, there are still many missing options. One of these is the possibility to automatically delete the images of the products in WoCommerce when a product is deleted.
Specifically, when we delete a WooCommerce product, the images associated with it remain on the server. At an online store with thousands of products, an important number of images will be gathered. If these pictures are not deleted with the products, then an important storage space will take care.
When in WooCommerce A new product is added, at least the presentation image will be duplicated by at least three – Four times, to different dimensions. There are some themes by Woo who can make up to 10 children of the original image, for different layouts.
The best solution for optimizing the space occupied on the webhosting server by the product images, is that they are deleted with the products removed from the online store.
How do you automatically delete product images in WooCommerce when deleting products
I have an online store that has in media library 23,567 images, most of the WooCommerce products. If I delete the products that are no longer in stock, the images would remain in the media library (on the server).

To automatically delete the images of the products in WooCommerce, with the products, all you have to do is add to the Functions.php file of the active theme, the following code:
*It is highly recommended to make a backup of the folder before wp-content/uploads
.
// Automatically Delete Woocommerce Images After Deleting a Product
add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
function delete_product_images( $post_id )
{
$product = wc_get_product( $post_id );
if ( !$product ) {
return;
}
$featured_image_id = $product->get_image_id();
$image_galleries_id = $product->get_gallery_image_ids();
if( !empty( $featured_image_id ) ) {
wp_delete_post( $featured_image_id );
}
if( !empty( $image_galleries_id ) ) {
foreach( $image_galleries_id as $single_image_id ) {
wp_delete_post( $single_image_id );
}
}
}
It is good to know that after you save the above code in functions.php
, product images will be erased automatically, with the product. When the products are deleted and from “Trash“.

With the products removed from the online store were erased and 3336 images associated. A rather important number, which would have occupied useless space on the web hosting server.
- How to customize the page "No Products Were Found Matching Your Selection" in WooCommerce - Optimizing sales on online stores
- How to clean huge wp_options in SQL – Transients, wpseo_sitemap _cache_validator
- How to quickly delete all commands (orders) in WooCommerce [sql tips]
- FIX HUGE SQL tables: wp_actionscheduler_actions & wp_actionscheduler_logs [WooCommerce Tips]
- Cum debifam implicit “Ship to different address” din pagina de Checkout a Woocommerce
Do not use this option if you use the same pictures for multiple products. These will be deleted automatically if a product is eliminated to which they are present.
It helped, thanks!
Thank you very much, you solved me a big problem.
Exactly what I was looking for or needed 👍
The plugin that I have used so far did not work as reliably as this snippet
Thank you very much, that saves me a lot of work and a lot of time