How to automatically delete product images in WooCommerce, together with the product

Even if WooCommerce is a very robust shopping platform that has developed a lot in recent years, there are still many missing options. One of these is the possibility to automatically delete the product images WooCommerce when a product is deleted.

More precisely, when we delete a product from WooCommerce, the images associated with it remain on the server. An online store with thousands of products will gather a significant number of images. If these pictures are not deleted together with the products, then over time it will take up a significant amount of storage space.

When in WooCommerce a new product is added, at least the presentation image will be duplicated at least three - four times, in different sizes. There are some themes by Woo that can make up to 10 copies of the original image, for different layouts.

The best solution for optimizing the space occupied by the product images on the web hosting server is for them to be deleted together with the products removed from the online store.

How to automatically delete product images in WooCommerce when you delete products

I have an online store that has flax media library 23.567 images, most of the products WooCommerce. If I were to 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 product images in WooCommerce, together with the products, all you have to do is add the following code to the functions.php file of the active theme:

*It is highly recommended to make a backup of the folder first 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 saving the above code in functions.php, product images will be deleted automatically, together with the product. When the products are also deleted from "Trash".

How to automatically delete product images in WooCommerce, together with the product
Delete WooCommerce Product Image

Together with the products removed from the online store 3336 images were also deleted associated. A rather important number, which would have occupied unnecessary space on the web hosting server.

Do not use this option if you use the same images for several products. They will be deleted automatically if a product in which they are present is removed.

Passionate about technology, I enjoy writing on StealthSettings.com since 2006. I have a rich experience in operating systems: macOS, Windows, and Linux, as well as in programming languages and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, PrestaShop).

How to » WordPress » How to automatically delete product images in WooCommerce, together with the product

3 thoughts on "How to automatically delete product images in WooCommerce, together with the product"

  1. Genau was ich gesucht bzw. need habe 👍

    Das PlugIn das ich bisher genutzt habe, funktionierte nicht so versältigte wie dieses Snippet

    Vielen Dank, das erspart mir einen Haufen Arbeit und viel Zeit

    Reply
Leave a Comment