How to Delete All Products WooCommerce MySQL

delete all products woocommerce mysqlIn my previous post on WordPress database, I illustrated how to access and manage WordPress database in depth. If you are looking for a way to delete all products WooCommerce using MySql, this post will guide you. I will share with you the delete all products woocommerce mysql query that you can run on your SQL wizard and successfully delete all products from WooCommerce database.

If you are have not interacted with WordPress database before, I have covered this topic here on this blog and explained how to connect WordPress to MySQL database among other ways to manage WordPress data from PHPMyAdmin.

Delete All Products WooCommerce MySQL

In most cases when you are developing a WooCommerce plugin, you may want to delete data for different reasons. You may also want to clean the database by deleting all the WooCommerce products along with the meta data saved in the database.

It is important to remember that the WooCommerce product is a custom post type as the other custom post types I illustrated in the tutorial on how to create custom post types in WordPress without using a plugin.

The product extra data is saved as the post meta and when you are deleting the product from the database using MySQL it’s important to have this perspective.

The following are the steps you should take to delete all products from WooCommerce database using MySQL query.

Login to WooCommerce Database ( Using MySQL management Tools || PHPMyAdmin )

First, you need to be logged in to your WooCommerce database. In most cases, you should be using PhpMyAdmin to access the database and this is well documented on the WordPress Database Guide from Novice to expert.

Before you go ahead to delete all products from the database, it is always advisable to back up the data to avoid loss in case you would want to restore the deleted product data.

Most people skip this step which is crucial and in my experience, you should always back up everything before you delete or update since it is easier to restore to the previous version if something fails as you run the SQL query.

Run SQL Query to Delete All WooCommerce Product MySQL

Now after creating the backup of your WooCommerce database, you can now comfortably run the following SQL query on the wizard and this will successfully delete all the products from your WooCommerce database.

[php] DELETE relations.*, taxes.*, terms.*
FROM wp_term_relationships AS relations
INNER JOIN wp_term_taxonomy AS taxes
ON relations.term_taxonomy_id=taxes.term_taxonomy_id
INNER JOIN wp_terms AS terms
ON taxes.term_id=terms.term_id
WHERE object_id IN (SELECT ID FROM wp_posts WHERE post_type=’product’);

DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = ‘product’);
DELETE FROM wp_posts WHERE post_type = ‘product’;

[/php]

It is also important to consider adjusting the wp table prefix in the SQL query to reflect the table prefix of your database.

If you don’t understand what wp_table prefix in WordPress database means check my explanation here – WordPress database table prefix. You should also be familiar with using most MySQL database management tools and particularly PHPMyAdmin.

Conclusion

In this post, we have looked at how to delete all WooCommerce products using MySQL query. This not only saves you time but also helps you to clean up your WooCommerce database and can be a useful query for WooCommerce developers who are building and testing out WooCommerce plugins using product data.

Similar Articles

  1. How to Create WooCommerce Category Loop Explained With Example