While building and testing WooCommerce addons, I find myself in need of quickly deleting all WooCommerce products from the database. WPCLI is the easiest way to go, as you can achieve the desired results via a single line.
NOTE: if your database prefix isn’t the default wp_, you’ll need to change it with your actual database prefix.
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 IN ('product','product_variation'));
DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
DELETE FROM wp_posts WHERE post_type IN ('product','product_variation');
PRO TIP:
If you are considering deleting items in WooCommerce, please be aware that this action cannot be undone. Once you have deleted an item, it will be permanently gone from your store. Before taking this action, please be absolutely sure that you no longer need or want the item in question.
Was this post helpful?
Let me know if you liked the post. That’s the only way I can improve. 🙂