Drupal - How to delete all terms from a vocabulary (bulk delete)?

A favorite method of bulk-deleting content by content type, taxonomy vocabulary, etc utilizes the http://drupal.org/project/devel module. For deleting all terms in a vocabulary:

  1. Enable the Devel and Devel Generate modules
  2. Go to Development > Generate Terms
  3. Select the Vocabulary from which you wish to delete terms
  4. Enter "0" for number of Terms to generate
  5. Check "Delete existing terms before generating new ones"
  6. Click Submit

Voila-- empty vocabulary, otherwise untouched.


Taxonomy manager does have bulk delete functionality, just select all the terms in your vocabulary and click the 'Delete' button:

enter image description here

If you want to do it using code something like the following should help:

$vocabulary = taxonomy_vocabulary_machine_name_load('my_custom_vocabulary');
foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
  taxonomy_term_delete($term->tid);
}

You can use the following command:

drush -v eval 'foreach(taxonomy_get_tree(123) as $term) { taxonomy_term_delete($term->tid); }'

If it doesn't work, make sure that you clear your cache (e.g. memcached).

Or faster way by using the following dirty SQL query:

drush sqlq "DELETE FROM taxonomy_term_data WHERE vid = 123"

Where 123 is your vocabulary id which you should change.

You can get vid of your vocabulary name by the following command:

drush sqlq "SELECT name, vid FROM taxonomy_vocabulary WHERE name = 'vocabulary_name'"

See also:

  • How to delete all terms in a vocabulary with drush?