Agaric Design Collective

Delete unused taxonomy terms from a free tagging vocabulary

Average: 5 (1 vote)
By Benjamin Melançon
on 30 Nov
3 comments

Key words and phrases

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

Tags

Description

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.

 

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.

Posted by Benjamin Melançon on Sun, 2008-12-07 10:38
Not confirmed

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

Posted by Stefan Freudenberg on Thu, 2009-07-23 15:28
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.