How to Automatically Delete Woocommerce Images After Deleting a Product?

Images are the biggest headache for a Woocommerce Website as it takes a lot of storage space in Server and eventually slow down the Website speed.

When you constantly Add New Products and discard old products, It creates the issue of Unused images. As you know when you delete a Product on your Woocommerce store, the images attached to it won’t be deleted from your Media Library.

So, More and more products you add to your online store, more images will be stored on your database and slows it down.

So, By using a code snippets like this, You will be able to clean up images that are currently not attached to any existing product pages. If you use the same image on multiple product pages then the image will not be deleted.

How to Automatically Delete Woocommerce Images After Deleting a Product?
Get Additional 20% Discount on Hostinger Hosting
YouTube video
How to Automatically Delete Woocommerce Images After Deleting a Product

⚠️ Before You Begin: Safety First

This code uses the wp_delete_attachment function with the “force” parameter set to true. This means images will be permanently deleted.

Make sure to Take a backup of your website before using any PHP codes. Also, Test the code in a dummy website with the same setup as live site first.

// Automatically Delete Woocommerce Images After Deleting a Product
add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
function delete_product_images( $post_id ) {
		if ( !current_user_can( 'delete_products' ) ) {
			return;
		}
    $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 ) ) {
        $is_featured_image_used = is_image_used( $featured_image_id, $post_id );
        if ( !$is_featured_image_used ) {
            wp_delete_attachment( $featured_image_id, true );
        }
    }

    if( !empty( $image_galleries_id ) ) {
        foreach( $image_galleries_id as $single_image_id ) {
            $is_image_used = is_image_used( $single_image_id, $post_id );
            if ( !$is_image_used ) {
                wp_delete_attachment( $single_image_id, true );
            }
        }
    }
}
function is_image_used( $image_id, $current_product_id ) {
    $query = new WP_Query( array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => '_thumbnail_id',
                'value' => $image_id,
                'compare' => '='
            ),
            array(
                'key' => '_product_image_gallery',
                'value' => '"'.$image_id.'"',
                'compare' => 'LIKE'
            )
        ),
        'post__not_in' => array( $current_product_id ),
        'fields' => 'ids',
        'posts_per_page' => -1
    ) );

    return ( $query->have_posts() );
}

How to Install the Code

You have two easy ways to add this to your WordPress site:

Using WPCode (Recommended): Install the WPCode plugin, create a new “PHP Snippet,” paste the code, and set it to “Active.”

Theme functions.php: Paste the code at the bottom of your child theme’s functions.php file.

All, You have to do is use this above php code on your Wordpress website. You can either use a code manager plugin or directly paste it inside the theme’s function.php file.

How to Automatically Delete Woocommerce Images After Deleting a Product?

Note: This only handles images stored in your local Media Library.

Once you Add the code on your Wordpress website, then it will auto delete all product images when you delete that products. In this way, You can free up your storage space and improve your Website Speed performance.

Need Professional Speed Optimization?

While cleaning up images is a great start, a truly fast WooCommerce store requires expert database tuning, CDN configuration, and advanced caching.

If you want us to handle the technical heavy lifting, check out our WordPress Speed Optimization Services. We’ll make sure your store loads instantly on all devices.

Similar Posts

Leave a Reply

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

Hostinger Hosting
Get 20% Discount on Checkout
Hostinger managed Wordpress hosting
Get 20% Discount on Checkout