How To Automatically Delete WooCommerce Images After Deleting a Product

Automatically Delete WooCommerce Images After Deleting a Product. WooCommerce website is a big problem for most users; even after deleting products, the Images are not deleted from the media. If your WooCommerce website is a single vendor and not too large, you can manually delete the picture. But if your WooCommerce website is multi-vendor, it is essential to delete the images and the product.

Automatically Delete WooCommerce Images After Deleting a Product

If you do not delete the Images, your storage will be full very quickly. In today’s post, I will learn How To Automatically Delete WooCommerce Images After Deleting a Product Without a Plugin. So let’s start without further ado.

To add the image auto-delete function, I will add some code on our WooCommerce website. You can add code in two ways. You can add code to function.php or use the Code Snippets plugin.

Add Code to function.php

Themes you are using must have child themes installed. Because if you add a function to the theme’s function.php when the theme’s update comes, all the processes will go away after the update. If your website does not have child installed, you can use the Code Snippets plugin.

Login to your WordPress dashboard. Click on Edit Themes from Appearance. Then select function.php for the child theme and add the code at the bottom. And click on Update File.

Add PHP code function.php

Install Code Snippets Free Plugin

Click Add New from the plugin. Search Code Snippets, Click Install and Active.

Install Code Snippets Free Plugin

Add Code to Snippets Plugin

After installing the Snippets plugin, you will see a new option in the dashboard, “Snippets.” Click Add New from Snippets. Enter the title, paste the code, click save changes.

Add PHP Code Snippets Plugin

Automatically Delete WooCommerce Images Code

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

Remember that if you have added one image to many products. If you delete any one product, the image will be deleted. The rest of the products will not show the image. To update the image every time for each product.

I hope today’s post has been helpful to you so far. If you like the post, please share. If you have any problems or misunderstandings, please comment. I will try to solve it.

You can read our other post about WooCommerce

Share your love
Default image
SM.Tanvir Hassan
I love technology and love to learn. This is my personal blog. I share with you everything I learn through this blog
Articles: 24

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.