![](https://gokhancelebi.net/wp-content/uploads/2022/08/logo-og.png)
There are three examples in this article.
<?php
include __DIR__ . "/../../wp-load.php";
#delete woocommerce products from array with all images
$products = [1,2,3]; // product sku's
foreach ($products as $product) {
$product_sku = $product;
$product_id = wc_get_product_id_by_sku($product_sku);
if(!$product_id){
echo "Product with SKU: " . $product_sku . " not found.<br>\n";
continue;
}
#delete product variations by product id
$variations = get_children(array(
'post_parent' => $product_id,
'post_type' => 'product_variation',
'fields' => 'ids'
));
foreach ($variations as $variation_id) {
wp_delete_post($variation_id, true);
}
#delete product images
$product_images = get_post_meta($product_id, '_product_image_gallery', true);
if($product_images){
$product_image_gallery = explode(',', $product_images);
foreach ($product_image_gallery as $image_id) {
wp_delete_attachment($image_id, true);
}
}
#delete product
if ($product_id) {
wp_delete_post($product_id, true);
echo "Product deleted: " . $product_sku . " " . $product_id . "<br>\n";
}
}
<?php
include __DIR__ . "/../../wp-load.php";
#delete woocommerce products from array with all images
$products = [1,2,3]; // product sku's
foreach ($products as $product_id) {
#delete product variations by product id
$variations = get_children(array(
'post_parent' => $product_id,
'post_type' => 'product_variation',
'fields' => 'ids'
));
foreach ($variations as $variation_id) {
wp_delete_post($variation_id, true);
}
#delete product images
$product_images = get_post_meta($product_id, '_product_image_gallery', true);
if($product_images){
$product_image_gallery = explode(',', $product_images);
foreach ($product_image_gallery as $image_id) {
wp_delete_attachment($image_id, true);
}
}
#delete product
if ($product_id) {
wp_delete_post($product_id, true);
echo "Product deleted: " . $product_sku . " " . $product_id . "<br>\n";
}
}
<?php
include __DIR__ . "/../../wp-load.php";
#delete woocommerce products from array with all images
$products = get_posts(array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids'
));
foreach ($products as $product_id) {
#delete product variations by product id
$variations = get_children(array(
'post_parent' => $product_id,
'post_type' => 'product_variation',
'fields' => 'ids'
));
foreach ($variations as $variation_id) {
wp_delete_post($variation_id, true);
}
#delete product images
$product_images = get_post_meta($product_id, '_product_image_gallery', true);
if($product_images){
$product_image_gallery = explode(',', $product_images);
foreach ($product_image_gallery as $image_id) {
wp_delete_attachment($image_id, true);
}
}
#delete product
if ($product_id) {
wp_delete_post($product_id, true);
echo "Product deleted: " . $product_sku . " " . $product_id . "<br>\n";
}
}