How To Delete All Terms In A Taxonomy

Here’s how to delete all the terms in a taxonomy, whether it be category, tags or a custom taxonomy. Most sites will never need to do this – and there are probably better ways to do this – but here’s the code anyway.

Backup your database before attempting this! You have been warned!

The following code can be added to functions.php or to your own custom functionality plugin. It only needs to be added temporarily. The next time a page is loaded in the admin section, the code will run and delete all the terms in the taxonomy (this may take some time).

Subsequent page loads will run the code again, but this should fail gracefully. Once you’ve verified the terms have been deleted you should remove this code.

The code is as follows:

function sjc_delete_terms() {
     if ( is_admin() ) {
          $terms = get_terms( 'category', array( 'fields' => 'ids', 'hide_empty' => false ) );
          foreach ( $terms as $value ) {
               wp_delete_term( $value, 'category' );
add_action( 'init', 'sjc_delete_terms' );
Replace ‘category’ in lines 3 and 5 with the taxonomy you wish to delete terms for. Unless of course you are trying to delete all terms in the category taxonomy!

We’re hooking into init (line 9) to run the sjc_delete_terms function. At that point custom taxonomies should be loaded, although it depends how they are added, so you may need to tweak the priority or even the hook used.

The sjc_delete_terms function uses is_admin (line 2) to ensure that the code only runs on admin page loads. The code then has two steps:

First, we use get_terms to retrieve an array of term ids that we are going to delete (line 3). Note, as well as passing ‘category’ as the taxonomy, we pass an array of 2 parameters:

  • fields is set to ‘ids’ so that we only get an array of ids, rather than an array of term objects. We only need the ids, so it’s overhead to get any more.
  • hide_empty is set to false, so that it returns all terms, regardless of whether there are any posts against them. By default it will ignore terms that aren’t used.

Second, we loop through the returned array of term ids and delete them using wp_delete_term (lines 4 to 6).

Other Ways To Delete Terms

Mario Peshev recently wrote about how to delete terms using a MySQL query.

Mario’s code only deletes terms with no items. You need to remove the “AND count = 0” from line 10 of his code to have the equivalent of the code above which deletes all terms.

Mario’s solution is the best way to do this for very large data sets, which would take a long time to run in PHP.

I tried the PHP / WordPress code above on a data set with 700 terms and it took 30 seconds to delete them. If you got too much more than that, you could possibly run into script execution timeout errors (depending on your server settings).

Mario’s solution takes less than half a second for the same data set.

So why would you use the code above rather than the MySQL query, when MySQL is so much faster? The only reason would be if you are not comfortable using MySQL – I suspect many WordPress users will be more comfortable dropping code into functions.php than they would be opening up PHPMyAdmin and entering queries.

Don’t Forget The Obvious Solution

People sometimes forget they can unlock their car by inserting the key and turning. Don’t forget that you can just delete terms through the taxonomy page in the admin area.

Going through 20 items at a time can be tedious when you have a lot of items to delete. Fortunately, you can go to the Screen Options button in the top right corner and change the 20 entries to something like 200. The page will load more slowly, but you’ll be able to bulk delete 200 entries at a time.

You’d only need to do this 4 times to reach 700 items, so this would be far more efficient that adding code to functions.php, etc or using MySQL queries in PHPMyAdmin.

Final Thoughts

So this is only useful for a medium data set, where there are too many to do by hand, but not enough to take minutes to run. That window is very small, so I suspect most people will be best served by deleting them manually, with a few people working with large datasets using MySQL.

I know there are people out searching for how to delete terms on plugin deactivation. This may be a solution for some of these people, although you can’t predict how many the user will have added, so it may be safer using the MySQL query.

3 responses on “How To Delete All Terms In A Taxonomy

  1. Brett

    Taxonomy and categorisation of content will only become more powerful for search as content actually becomes a priority of bad links. Thanks this has helped our developers.

  2. Cristiano

    Olá, não seria mais simples assim:

    $taxonomie = 'category';
    function sjc_delete_terms($taxonomie) {
         if ( is_admin() ) {
              $terms = get_terms($taxonomie, array( 'fields' => 'ids', 'hide_empty' => false ) );
              foreach ( $terms as $value ) {
                   wp_delete_term( $value, $taxonomie);
    add_action( 'init', 'sjc_delete_terms' );


Leave a Reply

Your email address will not be published. Required fields are marked *

Anti-Spam Quiz: