How to Remove Unused WooCommerce Images from Your WooCommerce Store & Dokan Marketplace

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

As an eCommerce owner, you must want to keep your site updated with the latest products that are more likely to be sold. To do this, you may often delete some products that are out-of-stock, backdated, or out of season.

For a WooCommerce store, you can easily do this using the default WooCommerce setting or running an SQL command.

However, the problem is that WordPress does not delete media upon deleting a post. This could be a major issue for an eCommerce site or marketplace with thousands 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 a 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 for various 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. However, 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

  • Improve site performance
  • Increase store speed by minimizing server storage
  • Keep the site lightweight
  • Create more space for further uploading
  • 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.

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's performance.

Dokan multivendor marketplace solution

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

Another problem can arise if someone uses the same images on multiple posts.

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

How to Remove Woocommerce Images After Deleting a Product?

This is an illustration for bulk delete WooCommerce products

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

Follow the below steps to remove non-used images during bulk deletes of 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.

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 ) {

    $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.

Are you running a marketplace like eBay? In the next part, we'll show you how to remove WooCommerce product images from your marketplace.

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-


add_action( 'before_delete_post', function( $id ) {
    $product = wc_get_product( $id );
    if ( ! $product ) {
    $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 ) {
        $products = $vendor->get_products();
        if ( empty( $products->posts ) ) {
        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 ) ) {
            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 ) ) {
        $gallery_image_ids = $product->get_gallery_image_ids();
        if ( empty( $gallery_image_ids ) ) {
        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.

📚 Also read: How to Set up GA4 to Track Your Ecommerce Website Performance.

Bonus: When You Need to Bulk Delete WooCommerce Products

This an illustration for 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's performance. But first, you need to decide which products you need to remove from your online shop.

We have highlighted the types 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: For products that are not available in stock and have 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 they work 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 for 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

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