If you need to remove all products from your WooCommerce store, either because you are developing, have made an import and want to delete it or want to start over, you can use the following snippet that I have found and tested and that goes much faster when running directly in the database.
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'; DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'product_variation'); DELETE FROM wp_posts WHERE post_type = 'product_variation';
Always remember to make a database copy before working with queries as well.
I’ve added that you also remove variations from the original code.
Leave a Reply