How to delete images when deleting a product in WooCommerce?

What is good for you can be unnecessary for others! That’s what I am seeing over the last few years even in plugin/software functionalities. Today I am going to write about the uploaded image for a product through WooCommerce or any multivendor plugin works on WooCommerce like Dokan.

When you are running a multivendor marketplace or an e-commerce store with WooCommerce then you are uploading tons of products with thousands of images. When you are uploading lots of products definitely you are deleting a huge number of products due to some reasons. After deleting every product when you check your media library you are seeing all those images which were used for your deleted products.

WordPress does not delete media upon deleting a post because Media Library built to re-use images/files to one or multiple posts. That’s really great for me because I use same feature image on multiple post 😛

Now, when it’s a multivendor website it is really hard to keep track of each and every vendor uploaded media. So, your vendor might be uploading 100 products per day and deleting a good number of products. Their product images are reducing your server space which is not good for a marketplace/e-commerce owner.

In that case, most of them will love to delete all the featured image and gallery image if the user/vendor delete any product.

I know you will ask "What if you are using same images on multiple posts?"

Yes, I will definitely provide a good solution to save your left and right hand 😀

Deleting attached images upon deleting a post/product

WordPress is amazing because it allows you to modify your own wish. I will use that facility to apply my solution. Yeas, you got me correctly!

I am going to use a hook which is `before_delete_post` and you can read details about this hook here.

Now, open your child theme’s functions.php file and paste the below code-

After saving the functions.php now permanently delete a product. You will see the magic. Let’s try and put your feedback in a comment.

23 thoughts on “How to delete images when deleting a product in WooCommerce?

      1. Hi,

        Thanks for your quick response!

        I inserted the code in the following path:
        /wp-content/themes/poza-child/functions.php

        I use Dokan multivendor plugin and Poza theme.
        The file was empty before.
        It still doesn’t work… 🙁

        What did I do wrong..?

    1. Hi Adi,

      This is a very powerful code for Dokan users by Nazmul, however; I found this hook worked for featured images, but not for gallery images for some reason so I modified it slightly and it works. You may want to give it a try too:

      I believe Nazmul may be changing the code to make it work for all the product images rather than just individual vendors so that it still works when the same product is sold by multiple vendors because in that case only the first vendor who listed that product will have its images and rest of the vendors will loose images attached to it, if this can be archived this will become the most perfect solution 🙂

      CODE:

      add_action( ‘before_delete_post’, function( $id ) {
      $product = wc_get_product( $id );
      if ( ! $product ) {
      return;
      }
      $product_thum_id_holder = [];
      $gallery_image_id_holder = [];
      $thum_id = get_post_thumbnail_id( $product->get_id() );
      $vendor = dokan()->vendor->get( dokan_get_current_user_id() );
      if ( ! $vendor instanceof Dokan_Vendor || $vendor->get_id() === 0 ) {
      return;
      }
      $products = $vendor->get_products();
      if ( empty( $products->posts ) ) {
      return;
      }
      foreach ( $products->posts as $post ) {
      $product_id = ! empty( $post->ID ) ? $post->ID : 0;
      if ( ! $product_id ) {
      continue;
      }
      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 ) ) {

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

      Thanks