mrtechhive title BG

Automatically delete WooCommerce website images after products are deleted to improve performance.


Are you looking for ways to improve the performance of your WooCommerce website? One of the best ways to do this is by automatically deleting images associated with products that have been deleted. While deleting images manually is an option, it can be time-consuming and tedious. Automating this process will save you time and help ensure that your website remains running efficiently. In this blog post, we’ll discuss the steps you can take to automatically delete WooCommerce website images after products are deleted. We’ll also explore the benefits of doing this and provide tips for implementing the process.

Why Automatically Delete WooCommerce Website Images?

Deleting images on your WooCommerce website can help improve performance by reducing the amount of data that needs to be stored and accessed by your server. Having too many images on your website can slow down loading speeds, which can impact user experience and cause customers to lose interest in your store. Automatically deleting images after products are deleted helps free up server resources, reducing strain on your hosting and helping your website run more efficiently. Additionally, it keeps your image library organized and easier to navigate, further improving performance.

How to Automatically Delete WooCommerce Website Images

The themes you are using must have child themes installed. Because if you add a function to the theme’s function.php when the theme’s update comes, all the processes will go away after the update. If your website does not have a child installed, you can use the Code Snippets plugin.

Add Code to Function.php

Login to your WordPress dashboard. Click on Edit Themes from Appearance. Then select function.php for the child theme and add the code at the bottom. And click on Update File.

Add Code to function
Install Code Snippets Free Plugin

Click Add New from the plugin. Search Code Snippets, Click Install and Active.

Add Code to Snippets Plugin

After installing the Snippets plugin, you will see a new option in the dashboard, “Snippets.” Click Add New from Snippets. Enter the title, paste the code, click save changes.

Automatically Delete WooCommerce Images Code
add_action( 'before_delete_post', 'delete_product_images', 10, 1 );

function delete_product_images( $post_id )
    $product = wc_get_product( $post_id );

    if ( !$product ) {

    $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 );

Remember that if you have added one image to many products. If you delete any one product, the image will be deleted. The rest of the products will not show the image. To update the image every time for each product.

Benefits of Automatically Deleting WooCommerce Website Images

Automatically deleting WooCommerce website images can be a great way to keep your content fresh and up-to-date. It can also help to reduce storage costs, as you won’t need to keep unused images on your server. Additionally, it can help speed up your website, as fewer images will need to be loaded. Automatically deleting images can also help ensure that your content is always up-to-date and relevant, as any outdated images will be removed quickly and easily. Overall, automatic image deletion can be a great way to save time and money, while keeping your website looking its best.


Automatically deleting WooCommerce website images can help keep your website organized and running smoothly. It can help save storage space and reduce the amount of manual labor required to keep your images up-to-date. By using this Code, you can easily automate this process. The code allows you to set rules and conditions that will delete images after they have been used or are no longer relevant. This ensures that your website stays organized and remains up-to-date with the latest images. Automatically deleting WooCommerce website images can also help improve the overall performance of your website as it reduces the amount of data being stored.

Leave a Reply

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