Complete Guide to Deleting Products and Their Images from WooCommerce

woocommerce delete product with images
10 November 2024

Here is the PHP code for automatically deleting products and their images from WooCommerce. This code helps you when you want to delete a product, and the related images should also be removed from the media library without leaving any unused files.


This code will first delete the images associated with the product before deleting the product from WooCommerce, which can prevent the need to manually clean up this media library.


Method One: Deleting Product and Images Using WooCommerce API


This method is specifically for when you want to delete products remotely using the API. With this method, you can delete products and their images without logging into the admin panel.


PHP Code for Deleting Product Along with Images via API


require 'path/to/woocommerce-api.php'; 
// Specify the path to the WooCommerce API library

use Automattic\WooCommerce\Client;

// WooCommerce API details
$woocommerce = new Client(
'https://yourwebsite.com',
'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // Consumer Key
'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // Consumer Secret
[
'version' => 'wc/v3',
]
);

function delete_product_with_images($product_id) {
global $woocommerce;

try {
// Fetch product details
$product = $woocommerce->get('products/' . $product_id);

// Deleting images
if (!empty($product->images)) {
foreach ($product->images as $image) {
$image_id = $image->id;
wp_delete_attachment($image_id, true); // Permanently delete the image
}
}

// Deleting the product
$woocommerce->delete('products/' . $product_id, ['force' => true]);
echo "Product and images deleted successfully.";

} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}

// Example usage:
delete_product_with_images(123); // Enter the product ID you wish to delete here

Method Two: Deleting Product and Images Without Using WooCommerce API


If you want to perform this task from within WordPress without the API, you can do so directly by using WordPress and WooCommerce functions. This method is simpler and does not require WooCommerce API.


PHP Code for Deleting Product and Images Without API


/**
* Deleting WooCommerce product along with its associated images without API
*
* @param int $product_id The product ID you want to delete
*/
function delete_product_with_images_direct($product_id) {
// Fetch product details
$product = wc_get_product($product_id);

if (!$product) {
echo "Product not found.";
return;
}

// Deleting the gallery images
$attachment_ids = $product->get_gallery_image_ids();
foreach ($attachment_ids as $attachment_id) {
wp_delete_attachment($attachment_id, true); // Permanently delete the gallery images
}

// Deleting the main product image (featured image)
$thumbnail_id = $product->get_image_id();
if ($thumbnail_id) {
wp_delete_attachment($thumbnail_id, true); // Permanently delete the featured image
}

// Deleting the product
wp_delete_post($product_id, true); // Permanently delete the product
echo "Product and images deleted successfully.";
}

// Example usage:
delete_product_with_images_direct(123); // Enter the product ID you wish to delete here

Explanations:



  • Deleting gallery images: The function get_gallery_image_ids retrieves all the gallery images of the product, which can be deleted using wp_delete_attachment.

  • Deleting the main image: Using get_image_id retrieves the featured image, and that will be deleted with wp_delete_attachment.

  • Deleting the product: Finally, the wp_delete_post function is used to permanently delete the product.


Both methods generally achieve similar results; you can delete products remotely using API or delete them directly from the WordPress admin panel. Each method is appropriate and depends on your specific needs, so choose one of these methods accordingly.