4

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.)

  • 1
    I edit my answer with another solution, take a look ;-) – Adrian Cid Almaguer Jan 18 '16 at 16:18
  • finally, do you solve your problem? – Adrian Cid Almaguer Jan 26 '16 at 23:40
  • Would you be interested in a Rules Component that you might be able to use as an "Operation" in your VBO view? – Pierre.Vriens Feb 26 '16 at 18:10
  • Thanks, actually I have already used a "simple" method for this and forgot to share it. I will share the solution soon. – herci Feb 28 '16 at 12:22
  • can you share your solution? – Adrian Cid Almaguer Jan 9 '17 at 18:23
4

A quick solution:

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

<?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)->fetchCol();

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

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

<?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' => 'Delete Unused Taxonomy Terms',
  'description' => 'Delete Unused Taxonomy Terms',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('delete_unused_taxonomy_terms_admin_settings'),
  'access callback' => 'user_access',
  'access arguments' => array('administer taxonomy'),
  'type' => MENU_NORMAL_ITEM,
  'file' => 'delete_unused_taxonomy_terms.admin.inc',
  );
  return $items;
}

File: delete_unused_taxonomy_terms.admin.inc

<?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, &$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)->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:

3

There's no need to inject code into your code base; Drush is your friend here.

Create a PHP script with the relevant PHP code that bulk deletes then execute the following in your Drupal environment:

drush php-script my_deletion_script.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.