Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'
@jfrez

This comment has been minimized.

Copy link

@jfrez jfrez commented Feb 26, 2016

this is great! thanks!

@miweb777

This comment has been minimized.

Copy link

@miweb777 miweb777 commented Apr 14, 2017

Nice one. Just had to adjust my table name from "wp_terms" to my database table name "wpdn_terms" and so forth.
Worked perfectly. Thankyou so much.

@ilyachase

This comment has been minimized.

Copy link

@ilyachase ilyachase commented May 8, 2018

Seems to be it's not deleting products since they are stored in wp_posts with post_type = product.

@mumaromania

This comment has been minimized.

Copy link

@mumaromania mumaromania commented Jun 25, 2018

Hi, i have a woocommerce website,
my database name is: limiykm_wp1
When i paste the code and change db name, like this below, it gives me the message:
Sorry, but your code causes a "Fatal error", so it is not applied!
Please, check the code and try again.

The code i did paste is this, as above only changed the db name:

query("DELETE a,c FROM limiykm_wp1 AS a LEFT JOIN limiykm_wp1_taxonomy AS c ON a.term_id = c.term_id LEFT JOIN limiykm_wp1_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'product_tag'"); $wpdb->query("DELETE a,c FROM limiykm_wp1 AS a LEFT JOIN limiykm_wp1_taxonomy AS c ON a.term_id = c.term_id LEFT JOIN limiykm_wp1_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'product_cat'"); Please help, i am no programmer, just a regular user.
@BrookeDot

This comment has been minimized.

Copy link

@BrookeDot BrookeDot commented Feb 28, 2019

Just an FYI there's a missing ; at the end of the second DELETE statement in remove-all-product-categories-tags.sql Cheers!

@hellerbenjamin

This comment has been minimized.

Copy link

@hellerbenjamin hellerbenjamin commented Mar 25, 2019

Thanks. Saved me some time looking up the structure!

@JakeQZ

This comment has been minimized.

Copy link

@JakeQZ JakeQZ commented Jul 15, 2020

There are a couple of issues with this where it could be improved:

  • It does not remove entries from the termmeta table, which will end up orphaned (and can build up to the millions on even a small project);
  • It removes the default WooCommerce category (ID = default_product_cat option).

I have nonetheless found it useful on a 'dev' box, particularly when working with 'product import'. Thanks :)

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