I have a large vocabulary, and admin views does not yet work for taxonomy. Taxonomy Manager does not do this as well. Are there any options for bulk term delete?
Taxonomy manager does have bulk delete functionality, just select all the terms in your vocabulary and click the 'Delete' button:
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);
}
-
Ok thanks, although Taxo Manager does not have a bulk select option, and if you have hundreds of terms you would have to check each one by one. – giorgio79 Jul 27 '12 at 9:09
-
Yeah that's kind of annoying actually, I might put in a feature request for that – Clive♦ Jul 27 '12 at 9:13
-
2Thanks for adding both the UI and code methods! Always very useful +1 – Matt Fletcher Oct 25 '13 at 10:00
-
5@giorgio79 most modern browsers support Shift+Click on checkboxes. If you click a checkbox, then Shift+Click on another checkbox in the same group, all checkboxes between those 2 will be selected/unselected. – Елин Й. May 19 '14 at 13:55
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:
- Enable the Devel and Devel Generate modules
- Go to Development > Generate Terms
- Select the Vocabulary from which you wish to delete terms
- Enter "0" for number of Terms to generate
- Check "Delete existing terms before generating new ones"
- Click Submit
Voila-- empty vocabulary, otherwise untouched.
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:
-
1
-
1
-
1You could even combine the two, if you're feeling frisky:
drush sqlq "DELETE FROM taxonomy_term_data WHERE vid = (SELECT vid FROM taxonomy_vocabulary WHERE name = 'vocabulary_name')"
– Kirkland Sep 24 '15 at 15:08
Admin views just provides preconfigured VBO views. VBO itself works fine with terms (or any other entity type) Install VBO yourself and create the view that you want, then use VBO to delete the terms.
To bulk delete all taxonomy terms within a vocabulary in Drupal 7, use the taxonomy_term_delete
function by looping thru all terms.
Consider the following example:
// Get metadata about the vocabulary from its machine name
$vocab = taxonomy_vocabulary_machine_name_load('TAXONOMY_MACHINE_NAME');
// Get a hierarchical representation of all terms
$terms = taxonomy_get_tree($vocab->vid);
// Loop thru all terms in the taxonomy, deleting each one
if (!empty($terms)) {
foreach ($terms as $term) {
taxonomy_term_delete($term->tid);
}
}
Even easier, if you have Drush and the Devel module installed, you can use the following command to bulk delete all terms in a taxonomy* from the comfort of your shell:
$ drush generate-terms TAXONOMY_MACHINE_NAME 0 --kill
*This assumes you have the Devel Generate module enabled, which can be done if needed:
$ drush en -y devel && drush en -y devel_generate
-
thanks for the drush generate-terms because it was blowing up when used from the UI – GiorgosK Nov 30 '17 at 5:46
I would recommend using Admin Views with VBO to get a view to replace the default display of Taxonomy terms.
What you have to do is the following:
- Enable those modules of course
- Enable the view called "Administration: Taxonomy terms"
- Add a column into the view called "Bulk operations: Taxonomy term"
- FLUSH CACHES - then go to Admin > Structure > Taxonomy > Specific Vocab
I've just written a blog post on how to add a button to delete all taxonomy terms here.
Essentially:
I’m using the jQuery Easy Confirm Dialog plugin for this. First download the library from here and put this in your themes js folder.
We can then add a “Delete All Terms” button with a little code in a custom module:
function hook_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'taxonomy_overview_terms':
if($form['#total_entries']) {
drupal_add_library('system', 'ui.dialog');
drupal_add_js(drupal_get_path('theme', 'YOUR_THEME_NAME').'/js/jquery.easy-confirm-dialog.js');
$js = 'jQuery(document).ready(function($){$(".confirm").easyconfirm({locale: { title: \'Delete all '.$form['#vocabulary']->name.' terms\', button: [\'No\',\'Yes\']}});});';
drupal_add_js($js, array('type'=>'inline'));
$form['actions']['delete_all'] = array(
'#markup' => '<a href="/admin/structure/taxonomy/'.$form['#vocabulary']->vid.'/delete-all" class="button confirm" title="Are you sure you want to delete all terms from the '.$form['#vocabulary']->name.' vocabulary?">Delete All Terms</a>',
'#weight' => 10,
'#attributes' => array('class' => array('button'))
);
}
break;
}
}
Now we need to define the path to our function to delete the terms:
function hook_menu() {
$items = array();
$items['admin/structure/taxonomy/%/delete-all'] = array(
'title' => 'Delete all taxonomy terms',
'type' => MENU_CALLBACK,
'page callback' => 'delete_all_taxonomy_terms',
'page arguments' => array(3),
'access arguments' => array('administer taxonomy'),
);
return $items;
}
And finally add the functionality to actually delete the terms:
function delete_all_taxonomy_terms($vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('vid', $vid)
->execute();
foreach($result['taxonomy_term'] as $term) {
taxonomy_term_delete($term->tid);
}
drupal_set_message('All terms have been deleted from the '.$vocabulary->name.' vocabulary');
drupal_goto('admin/structure/taxonomy/'.$vocabulary->machine_name);
}
Just to complete the answers there is a module doing exactly this. It is the taxonomy_delete_all_terms module. I 've used it and it works.
For sites with very large Taxonomy vocabularies, deleting vocabularies may become impossible due to the term deletion request timing out. When that happens before the deletion transaction is complete, the transaction is rolled back, causing no terms at all to be deleted.
My answer is tangentially-related to the original question. Building on kenorb's answer, if you wish to clean ALL vocabularies at your site, you can run the following:
drush -v eval '$vocabularies = taxonomy_get_vocabularies(); foreach($vocabularies as $vocabulary) { foreach(taxonomy_get_tree($vocabulary->vid) as $term) { taxonomy_term_delete($term->tid);}}'
As was said by @texas_bronius, if you have devel generate enabled, you can use that but taking it one step further, if you also have drush installed, I use this command:
drush generate-terms [vocabulary_machine_name] 0 --kill
And just replace [vocabulary_machine_name] with the machine name of your vocabulary. The "0" is for how many terms to add and "--kill" is to remove the ones that are currently in there.