How do you automatically delete product images in WooCommerce, with the product

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).

Product Images in Media Library
Product Images in Media Library

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“.

How do you automatically delete product images in WooCommerce, with the product
Delete WooCommerce Product Image

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.

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.

Passionate about technology, I write with pleasure on stealthsetts.com starting with 2006. I have a rich experience in operating systems: Macos, Windows and Linux, but also in programming languages ​​and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, Presashop).

Home Your source of IT tutorials, useful tips and news. How do you automatically delete product images in WooCommerce, with the product

3 thoughts on “How do you automatically delete product images in WooCommerce, with the product”

  1. 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

    Reply
Leave a Comment