How to bulk delete empty taxonomy terms? The website has thousands of unused (empty) taxonomy terms and I want to bulk delete them.

With views all empty terms can be listed but it's not possible to delete them via Views (VBO etc).

I saw that taxonomy terms should be cleaned with taxonomy_term_delete() in this question: How to delete true orphaned taxonomy terms. But I don't know where to use it? Should I create a block and put the code inside it or a custom module or anything else?

(I also tried the Delete Orphaned Terms module but couldn't be sure if it's working properly. I tried it on a dev. site but couldn't check the results.)
A quick solution:

Go to your folder theme and edit the node.tpl.php and put this code at the top of the file:

&lt;?php

$query = "
SELECT taxonomy_term_data.tid AS tid
FROM 
{taxonomy_term_data} taxonomy_term_data
LEFT JOIN {taxonomy_index} taxonomy_index ON taxonomy_term_data.tid = taxonomy_index.tid
LEFT JOIN {node} node_taxonomy_index ON taxonomy_index.nid = node_taxonomy_index.nid
LEFT JOIN {taxonomy_vocabulary} taxonomy_vocabulary ON taxonomy_term_data.vid = taxonomy_vocabulary.vid
GROUP BY tid
HAVING COUNT(DISTINCT node_taxonomy_index.nid) = '0'
";

$tids = db_query($query)-&gt;fetchCol();

foreach ($tids as $tid) {
  taxonomy_term_delete($tid);
}
dpm($tids);
?&gt;


Then go to visit one of your nodes and the code will be triggered. 

Take into account that if you don't eliminate the code from the node.tlp.php then every time that you access a node the code will be triggered.

EDIT: I (quickly) created a little module for you. Is not the best solution but I tested it and it works.

file: delete_unused_taxonomy_terms.info

name = Delete Unused Taxonomy Terms
description = Delete all unused taxonomy terms
core = 7.x
package = Administration

configure = admin/structure/delete_unused_taxonomy_terms

files[] = delete_unused_taxonomy_terms.module
files[] = delete_unused_taxonomy_terms.admin.inc


file: delete_unused_taxonomy_terms.module

&lt;?php

/**
 * @file delete_unused_taxonomy_terms.module
 */

/**
* Implements hook_menu().
*/
function delete_unused_taxonomy_terms_menu() {
  $items['admin/structure/delete_unused_taxonomy_terms'] = array(
  'title' =&gt; 'Delete Unused Taxonomy Terms',
  'description' =&gt; 'Delete Unused Taxonomy Terms',
  'page callback' =&gt; 'drupal_get_form',
  'page arguments' =&gt; array('delete_unused_taxonomy_terms_admin_settings'),
  'access callback' =&gt; 'user_access',
  'access arguments' =&gt; array('administer taxonomy'),
  'type' =&gt; MENU_NORMAL_ITEM,
  'file' =&gt; 'delete_unused_taxonomy_terms.admin.inc',
  );
  return $items;
}


File: delete_unused_taxonomy_terms.admin.inc

&lt;?php
/**
* @file
* Calls results to administration's pages.
*/

/**
* Form constructor for module configuration.
*
*/
function delete_unused_taxonomy_terms_admin_settings($form_state) {
  $form['#submit'][] = 'custom_form_submit';
  return system_settings_form($form);
}

/**
 * Submit handler
 */
function custom_form_submit($form, &amp;$form_state) {
  $query = "
  SELECT taxonomy_term_data.tid AS tid
  FROM 
  {taxonomy_term_data} taxonomy_term_data
  LEFT JOIN {taxonomy_index} taxonomy_index ON taxonomy_term_data.tid = taxonomy_index.tid
  LEFT JOIN {node} node_taxonomy_index ON taxonomy_index.nid = node_taxonomy_index.nid
  LEFT JOIN {taxonomy_vocabulary} taxonomy_vocabulary ON taxonomy_term_data.vid = taxonomy_vocabulary.vid
  GROUP BY tid
  HAVING COUNT(DISTINCT node_taxonomy_index.nid) = '0'";

  $tids = db_query($query)-&gt;fetchCol();

  foreach ($tids as $tid) {
    taxonomy_term_delete($tid);
  }
}


In your module folder make a folder named delete_unused_taxonomy_terms put the 3 files inside and install the module.

Then go to admin/structure/delete_unused_taxonomy_terms and click the button.

Reference:


How to delete true orphaned taxonomy terms
Create a Views that displays all unused taxonomy terms