In this tutorial, I explain how to properly delete products & their attributes in WooCommerce using two different methods, one using built in WordPress functions and the other using SQL commands.
How to Delete All Products & Attributes with WordPress
To delete all products in WooCommerce using WordPress functions, you can use the following steps:
- Backup your database. It’s important to create a backup of your database before making any changes, as you could potentially lose data if something goes wrong.
- In the WordPress dashboard, go to WooCommerce > Products.
- Select all products by clicking the checkbox at the top of the list.
- In the Bulk Actions dropdown, select “Move to trash” and click the Apply button.
This will move all of the products to the trash, which you can then empty by going to the Trash page in the WordPress dashboard (located under the Posts menu).
How to Delete All Products & Attributes with SQL
Alternatively, you can use the wp_delete_post function to delete the products programmatically. This function takes a post ID as an argument and deletes the post from the database. You can use the get_posts function to retrieve all products and then loop through the products and delete them one by one using the wp_delete_post function.
Here’s an example of how you can use these functions to delete all products:
// Get all products
$products = get_posts(array(
'post_type' => 'product',
'numberposts' => -1
));
// Loop through the products and delete them
foreach ($products as $product) {
wp_delete_post($product->ID, true);
}This will delete all products permanently from the database. Keep in mind that this will also delete any data associated with the products, such as attributes, variations, and product categories and tags.
I hope this helps! Let me know if you have any questions.
Leave a Reply