Even if WooCommerce is a very robust shopping platform that has developed a lot in recent years, there are still many missing options. One of these is the possibility to automatically delete the product images WooCommerce when a product is deleted.
More precisely, when we delete a product from WooCommerce, the images associated with it remain on the server. An online store with thousands of products will gather a significant number of images. If these pictures are not deleted together with the products, then over time it will take up a significant amount of storage space.
When in WooCommerce a new product is added, at least the presentation image will be duplicated at least three - four times, in different sizes. There are some themes by Woo that can make up to 10 copies of the original image, for different layouts.
The best solution for optimizing the space occupied by the product images on the web hosting server is for them to be deleted together with the products removed from the online store.
How to automatically delete product images in WooCommerce when you delete products
I have an online store that has flax media library 23.567 images, most of the products WooCommerce. If I were to delete the products that are no longer in stock, the images would remain in the media library (on the server).
To automatically delete the product images in WooCommerce, together with the products, all you have to do is add the following code to the functions.php file of the active theme:
*It is highly recommended to make a backup of the folder first wp-content/uploads
.
// 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 );
}
}
}
It is good to know that after saving the above code in functions.php
, product images will be deleted automatically, together with the product. When the products are also deleted from "Trash".
Together with the products removed from the online store 3336 images were also deleted associated. A rather important number, which would have occupied unnecessary space on the web hosting server.
- How to customize the page "No products were found matching your selection" in WooCommerce - Optimizing Sales on Online Stores
- How to clean huge wp_options in SQL - Transients, wpseo_sitemap _cache_validator
- How to quickly delete all orders (Orders) from WooCommerce [SQL Tips]
- FIX HUGE SQL tables: wp_actionscheduler_actions & wp_actionscheduler_logs [WooCommerce tips]
- How to uncheck default “Ship to different address” from the Woocommerce Checkout page
Do not use this option if you use the same images for several products. They will be deleted automatically if a product in which they are present is removed.
This helped, thanks!
Grazie mille, you solved a big problem for me.
Genau was ich gesucht bzw. need habe 👍
Das PlugIn das ich bisher genutzt habe, funktionierte nicht so versältigte wie dieses Snippet
Vielen Dank, das erspart mir einen Haufen Arbeit und viel Zeit