Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Created January 27, 2016 11:37
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save mikaelz/9f98dcf9664c51e88900 to your computer and use it in GitHub Desktop.
Save mikaelz/9f98dcf9664c51e88900 to your computer and use it in GitHub Desktop.
Remove all WooCommerce product categories and tags
<?php
require dirname(__FILE__).'/wp-blog-header.php';
$wpdb->query("DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = 'product_tag'");
$wpdb->query("DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = 'product_cat'");
DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = 'product_tag';
DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE c.taxonomy = 'product_cat'
@JakeQZ
Copy link

JakeQZ commented Aug 26, 2023

Would be nice to have some commentary on what the SQL queries are doing. I am getting old and spent 20 years away from databases ;)

@gnanet
Copy link

gnanet commented Aug 26, 2023

To understand them, you go from bottom first, and then top2down:

  • the first query filters for product_tag as taxonomy type, and deletes all terms, the connected taxonomies, and their relation-data
  • the second query filters for product_cat as taxonomy type, and deletes all terms, the connected taxonomies, and their relation-data

To be honest, as i understand the docs, both queries specifiy to delete rows in terms and term_taxonomies only by writing a,c

@JakeQZ
Copy link

JakeQZ commented Aug 26, 2023

To be honest, as i understand the docs

The docs on the database are few and far between, IIRC.

The solution here leaves orphans in other tables, so is not complete. The orhpans should be perhaps have been linked via a foreign key by the original programmers to prevent such issues, but we are where we are.

@gnanet
Copy link

gnanet commented Aug 26, 2023

To be honest, as i understand the docs

The docs on the database are few and far between, IIRC.

The solution here leaves orphans in other tables, so is not complete. The orhpans should be perhaps have been linked via a foreign key by the original programmers to prevent such issues, but we are where we are.

I am actually woking on a project, where i need a good bunch of cleanup sql-queries, to get rid of all product, attribute category etc and also orders and customers. but the article below seems outdated:
https://www.businessbloomer.com/woocommerce-database-explained-how-it-works-and-where-to-find-data/

simply by looking i found **wc_product_attributes_lookup ** also worth for cleaning :(

if i finish the cleanup, ill try to summarize it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment