Set WordPress To Delete Images When I Delete A Product

adminJanuary 1, 20261 views

The Problem

Anyone who runs a WooCommerce store within WordPress will know that when you delete a product from your store unfortunately the images remain in your media library and are not removed with the product.

Over time this can start to use up serious amounts of web space with images that are no longer used for your products. Also, remember that for each image you upload WordPress will make between 5 and 10 copies of that image in different sizes to accommodate things like thumbnails so the numbers of unused images can really start to add up.

This also makes looking for images manually much more difficult as you end up scrolling through hundreds of unused images.

The Solution

We can if you wish add code to your site which will automatically remove the images attached to a product when you delete that product.

How It Works

Once we have added the code to your site the following actions will remove associated images

1. First delete the product. Note that this simply moves the product to the trash/bin section. At this point, the images will not be removed and will still be in your media library.

2. After you have deleted the product go to the trash/bin section and permanently remove the product to complete the process.

After the permanent deletion of the product, all images including the multiple sized versions WordPress created will be removed.

WARNING!

You need to make sure that you do not assign one image to multiple products, if you do, deleting one product will also remove the image from the other product. In reality, you should not come across this situation as most products have their own images. If you have two products with the same image a workaround is to simply upload the image twice with slightly different file names so that each product in essence has its own images.

How To Get This Functionality

If you would like us to add this custom code to your site simply pop in a support ticket as a work request and we will set it up for you. The cost will be £7.50 one time. A small price to save you hours of work and save your valuable webspace.