Drupal - How to delete all terms in a vocabulary with drush?

Untested; use for inspiration and test:

<?php

$vid = 17;

$tree = taxonomy_get_tree($vid);

if (count($tree) == 0) {
  print "Nothing to delete.\n";
}
else {
  $tree = array_slice($tree, 0, 1000);

  foreach ($tree as $term) {
    print 'Deleting tid ' . $term->tid . "\n";
    taxonomy_term_delete($term->tid);
  }
}

Put this in its own file, and execute as

drush -u 1 scr nuke_vid_18.php

Keep running it until it doesn't show anything as deleted. The array_slice is there to limit things so you don't run out of memory. You can potentially save some memory by querying directly, but taxonomy_get_tree() isn't terribly inefficient as long as you aren't doing full term loads.

Backup your database with drush sql-dump as a precaution before you do anything like this.


I've these modules: Drush and Devel For exclude all terms of taxonomy, use the generate-terms command:

drush generate-terms you_vocabulary_name 0 --kill

Change your_vocabulary_name to name of vocabulary you need delete terms


You can use the following command:

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

Where 123 is your vocabulary id which you should change.

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

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 from a vocabulary (bulk delete)?