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

Even though 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 in 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 WooCommerce products. 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.

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

Passionate about technology, I like to test and write tutorials about operating systems macOS, Linux, Windows, about WordPress, WooCommerce and configure LEMP web servers (Linux, NGINX, MySQL and PHP). I write on StealthSettings.com since 2006, and a few years later I started writing on iHowTo.Tips tutorials and news about devices in the ecosystem Apple: iPhone, iPad, Apple Watch, HomePod, iMac, MacBook, AirPods and accessories.

Leave a Comment