How to delete all products, categories, product tags and product attributes from woocommerce
If you want to delete all your products from a WooCommerce installation in order to upload a brand new product list or catalogue then run the following code in the MySQL database.
Thanks for this code to Ciprian and Marcvanzee from WordPress.org .
As Marcvanzee explain: “The idea is that you join the table that contains the relationships between posts and taxonomies (wp_term_relationships) with the table containing the taxonomies (wp_term_taxonomy). Then you join this with the table that contains the terms (wp_terms). This results in all terms and taxonomies that are used by posts. The WHERE clause makes sure that you will only remove the taxonomies and terms of products”
So assuming that you have the standard tables names you can execute the next queries in the phpmyadmin or other inteface to db:
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_term_relationships 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';
IMPORTANT NOTE: Beware that this query might also remove taxonomies and terms that are used by pages that are not products as well, so if you want to filter this out you need an additional query.