User login

Delete unused taxonomy terms from a free tagging vocabulary

Wish taxonomy terms would get deleted when a node is deleted, if they aren't used by any other content?

Agaric brings you the next best thing.

@TODO ben-agaric: turn this into a module

Basically we want to delete any term that doesn't exist in the term_node table, though it would be nice to do this by vocabulary (as in, nice not to accidentally delete your carefully set up non-free tagging vocabulary)

SQL to do this (with Drupal table prefix enabling brackets and using aliases, which (in MySQL) can only be defined after the word using and, if defined, must be used in the DELETE FROM part of the phrase):

db_query("DELETE FROM td USING {term_data} td LEFT JOIN {term_node} tn ON td.tid = tn.tid WHERE tn.tid IS NULL AND td.vid = %d", $vid);

Reference:
http://www.informit.com/articles/article.aspx?p=377653&seqNum=10&rl=1

The way the term_node table works (albeit obvious) is pointed out explicitly here: http://groups.drupal.org/node/2534

But I'm really linking there for this jewel (completely unrelated to the topic of this post, but cool):

Question: Is it possible to set whether a taxonomy is required by a nodetype? The required setting is global.

Answer: But of course... it's very easy to change with FormAPI, by removing the "none" value, or set required to be true in the Dojo module for the hook_form_alter() for battle forms.

if ($form_id == 'battle_node_form') {
// substitute vid for Battle Type on your system
// on Josh's it was 2
unset($form['taxonomy'][7]['#options'][0]);
$form['taxonomy'][7]['#required'] = TRUE;
}

You could have an admin page, with an array of checkboxes to optionally require vocabaularies for given node types.

There really does need to be a taxonomy utility module. Deleting terms that are no longer associated with a node is arguably within the jurisdiction of Agaric's Edit Term module, but making taxonomy required or optional by node type? Hmm.

Resolution

Searched words: 
drupal delete taxonomy terms not associated with any node remove unused taxonomy terms for a vocabulary MySQL delete where join no match Delete table alias mysql drupal taxonomy miscellaneous module drupal taxonomy utility module

Comments

See also

Looking for a module that deletes orphan free tags.

Comments suggest that Taxonomy Manager can do this now. Another comment confirms this approach.

Not confirmed

One commenter suggested Taxonomy Manager could do that but never answered questions how this can be done. Probably a false statement.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.