How to Remove Unused WooCommerce Images from Your Dokan Marketplace

Looking for solutions to remove images while doing bulk delete WooCommerce products?

While running an online business, you must be vigilant. You should be aware of everything like trending fashion, new trends, and the outdated products of your store. You can keep your site updated with the latest products that are more likely to be sold.

In order to keep your store up-to-date, sometimes you need to delete some products that have not been sold for a long time or products that are out of season. For a WooCommerce store, you can easily do this using the default WooCommerce setting or running an SQL command.

But the problem is WordPress does not delete media upon deleting a post. This could be a major issue for an eCommerce site or marketplace having thousand of high-quality images.

In the following blog, you'll learn how to remove WooCommerce images from the gallery when deleting a product for both a single store and multivendor marketplace.

Why You Should Remove WooCommerce Product Images after Deleting That Product

You need to upload plenty of products with thousands of related images for your eCommerce store. And sometimes it's also very normal to delete some products from your site due to some reasons. After deleting a product if you check your media library you will find all those images which were used for your deleted products.

This means that after deleting a product in WooCommerce, the system removes most of the data associated with it. But the image of that product will remain in the database and the media folder. It will make your site unnecessarily heavy.

Benefits of Removing Product Images after Deleting That Product
1. Improve site performance
2. Increase store speed by minimizing server storage
3. Keep the site lightweight
4. Create more space for further uploading
5. Optimize the store database

For marketplace owners, the scenario is a little bit more complex. It's severely difficult to monitor all the media files uploaded by different vendors. Since a vendor possibly uploads a good number of products every day and also deletes many of the existing ones. In turn, their product images may reduce your server space which is not good for your site performance.

In that case, it would be beneficial to delete all the featured image and gallery image if the user/vendor delete any product.

But another problem can arise there if someone uses the same images on multiple posts.

Well, today we'll show you solutions to manage all these obstacles.

How to Delete Woocommerce Images After Deleting a Product?

bulk delete WooCommerce products

Here, we're going to use the hook: before_delete_post. Check the details about this hook here.

Follow the below steps to remove non-used images during bulk delete WooCommerce products:

  • Step1: Create a Child Theme
  • Step2: Implement The Code Inside Your Child Theme
  • Step3: Check the Result for Your Online Store

Step 1: Create a Child Theme

Firstly, create a child theme. You need to modify this child theme since this enhancement is related to the theme.

Create a folder for child theme

You can follow this guide to create a child theme with ease. Or use any WordPress plugin to create a child theme.

Step 2: Implement The Code Inside Your Child Theme

Next, open your child-theme functions.php and paste the below code-

// 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 );
        }
    }
}

Step 3: Check the Result for Your Online Store

After inserting the code save the functions.php. Now go to your WooCommerce shop and delete a product. It will delete the associated images as well.

delete images

How to Remove WooCommerce Product Images in Dokan Marketplace

Now we'll show you how to delete Woocommerce images after deleting products inside a marketplace. It helps you free up server space and reduce media library clutter.

In this case, paste the below code snippet inside your theme's functions.php file-

<?php

add_action( 'before_delete_post', function( $id ) {
    $product = wc_get_product( $id );
    if ( ! $product ) {
        return;
    }
    $all_product_ids         = [];
    $product_thum_id_holder  = [];
    $gallery_image_id_holder = [];
    $thum_id                 = get_post_thumbnail_id( $product->get_id() );
    if ( function_exists( 'dokan' ) ) {
        $vendor = dokan()->vendor->get( dokan_get_current_user_id() );
        if ( ! $vendor instanceof WeDevs\Dokan\Vendor\Vendor || $vendor->get_id() === 0 ) {
            return;
        }
        $products = $vendor->get_products();
        if ( empty( $products->posts ) ) {
            return;
        }
        foreach ( $products->posts as $post ) {
            array_push( $all_product_ids, $post->ID );
        }
    } else {
        $args     = [ 'posts_per_page' => '-1' ];
        $products = wc_get_products( $args );
        foreach ( $products as $product ) {
            array_push( $all_product_ids, $product->get_id() );
        }
    }
    foreach ( $all_product_ids as $product_id ) {
        if ( intval( $product_id ) !== intval( $id ) ) {
            array_push( $product_thum_id_holder, get_post_thumbnail_id( $product_id ) );
            $wc_product        = wc_get_product( $product_id );
            $gallery_image_ids = $wc_product->get_gallery_image_ids();
            if ( empty( $gallery_image_ids ) ) {
                continue;
            }
            foreach ( $gallery_image_ids as $gallery_image_id ) {
                array_push( $gallery_image_id_holder, $gallery_image_id );
            }
        }
    }
    if ( ! in_array( $thum_id, $product_thum_id_holder ) && ! in_array( $thum_id, $gallery_image_id_holder ) ) {
        wp_delete_attachment( $thum_id, true );
        if ( empty( $thum_id ) ) {
            return;
        }
        $gallery_image_ids = $product->get_gallery_image_ids();
        if ( empty( $gallery_image_ids ) ) {
            return;
        }
        foreach ( $gallery_image_ids as $gallery_image_id ) {
            wp_delete_attachment( $gallery_image_id, true );
        }
    }
} );

Now, whenever any vendor deletes a product from the marketplace, the associated product images will automatically vanish from the media library.

Bonus: When You Need to Bulk Delete WooCommerce Products

bulk delete WooCommerce products

It's a good idea to remove unused Images from your media gallery. These images add no value to your store. Rather it hampers your site performance. But first, you need to decide which products you need to remove from your online shop.

We have highlighted type of products you may need to bulk delete-

  • Seasonal products
  • Cyclic products
  • Out of stock products
  • Non-selling products
  • Test products

Seasonal products: Some products may lose their demand after a specific time period. You can add new winter products, for example, in your store targeting the coldest time period of the year. But after the arrival of spring, the demand for such products would fall and soon they would be totally out of trend. And you need to bulk delete WooCommerce products from your shop.

Cyclic products: Inventory needs to be replenished with new products at regular intervals. For example, after a special festival, you have to remove the old products from the store and upload new ones.

Out of stock products: Products that are not available in the stock and less possibility to restock again, it would be better to get rid of those products.

Non-selling products: If you have products in your stock that are not commonly sold, you should delete those products from your store. Because these products will only increase your liability and waste your space.

Test products: Many times before launching the original product you have to introduce many test products to see if it works well. Once you launch the main product, it is better to delete the test products.

Related Resource: Types of Products You Can Create Using WooCommerce!

Over to You

There could be several situations when you need to bulk delete WooCommerce products. And it would not be difficult as a WordPress user. But you face difficulties when after deleting a product, the related product images still remain in the media library.

The above guide helps you include an automatic process to delete images when deleting a product in WooCommerce.

Do you still have questions about how to bulk delete WooCommerce products and product images with action hooks? Let us know in the comments!

Subscribe to weDevs blog

We send weekly newsletter, no spam for sure

Related Post

Sabirah Islam
Written by

Sabirah Islam

Sabirah Islam is a creative content writer who loves to work on diverse topics. She has a deep interest to work with new marketing strategies and different buyer persona. In free times she loves to play with her twin boys.

Have something to say? Cancel Reply

Your email address will not be published.

Table of Contents