Looking for a module that deletes orphan free tags.

public
Ki - Fri, 2008-11-21 18:59

Is there a module that goes about cleaning up "orphan" free tags, i.e. free tags that are not associated with any nodes?

In general, can you share a tip to searching for modules?

Thanks.

It wouldn't take much

Aaron Hawkins's picture
Aaron Hawkins - Fri, 2008-11-21 21:02

I can't find any info about a module like that that has been built, but it wouldn't take much to do it. I would say you would just need to create a menu callback function that queries the term_data table and the term_node table and in a "while" loop deletes terms that don't have a tid in the term_node table. You could even automate it by using hook_cron. If you can't build it yourself I could do it for you for $200 or so.

Contact me on my form if you are interested.


Taxonomy manager

billfitzgerald's picture
billfitzgerald - Fri, 2008-11-21 22:55

Taxonomy manager:

http://drupal.org/project/taxonomy_manager

Cheers,

Bill

FunnyMonkey
Click. Connect. Learn.


How?

szy@drupal.org's picture
szy@drupal.org - Sat, 2009-02-07 03:02

Could you specify how Taxonomy Manager deletes orphan tags?

Szy.


in D6 Taxonomy Manager?

Matt V.'s picture
Matt V. - Fri, 2009-04-03 15:19

Yeah, I just tried installing the Drupal 6 version of Taxonomy Manager and I don't see any features for deleting orphaned terms. Was it maybe a feature in the D5 version? Or am I missing something?


Simple to do with SQL

Cary Gordon's picture
Cary Gordon - Sat, 2008-11-22 18:00

There is a good example at: http://michaelosmith.com/node/10

The caveats are:

  1. Backup before you do this! Particularly the first time.
  2. Be careful when deleting terms in the forums vocabulary, as this can have nasty consequences.

We usually do this king of thing by running SQL scripts either from the command line or via cron. You can certainly do it through PHPMyAdmin if you have that available, but I don't recommend the use of that kind of tool on production sites, as it is very easy to do something regrettable.


As SQL in a Drupal db_query function

Benjamin Melançon's picture
Benjamin Melançon - Wed, 2008-12-10 03:43

Delete unused taxonomy terms from a free tagging vocabulary. Same approach Cary Gordon sites I think and same caveats apply!


Here's a little module,

sapark@drupal.org - Thu, 2009-02-12 14:12

Here's a little module, though it is just for all vocabularies. It could be modified to delete terms from certain vocabularies with function taxonomy_node_get_terms_by_vocabulary() where I think you could replace
$terms = taxonomy_node_get_terms($node);
with
$terms = taxonomy_node_get_terms_by_vocabulary($node, 1)
to delete terms from just vocabulary 1. There's no interface.

So to create this module from scratch, create a file called deleteterms.module and copy paste this--I think without the ending '?>'-- I have mine included but I think for good programming it's supposed to be removed:

<?php
// Delete a node's unused taxonomy terms when the node is deleted or updated.

function deleteterms_nodeapi ($node, $op, $a3 = NULL, $a4 = NULL) {
   if (
$op == "delete" || $op == "update") {
     
// Get the terms that were on the deleted or updated node
    
$terms = taxonomy_node_get_terms($node);  
     
// See if any nodes use the terms. If none, delete the terms.
     
foreach ($terms as $term) {
         if (
taxonomy_term_count_nodes($term->tid) == 0) {
           
taxonomy_del_term($term->tid);
         }
      }
   }    
}
?>

Then create another file called deleteterms.info and copy and paste this:

; $Id$
name = Delete terms
description = Delete unused taxonomy terms when a node is deleted or updated.
core = 6.x
version = 6.x-1.0

And finally create a folder in sites/all/modules called deleteterms and copy these files there, enable, and go.

This really good particularly with the jump module and views with exposed filters where they're providing a select list and you don't want to list terms that don't go to any nodes. I was really worried that someone would save a node with a misspelled term then go back and change it, and the misspelled term would still be in the list for users to scratch their heads over-- and then also someone else would accidentally pick it from the autocomplete when creating a new node.

I got the lovely idea of this module from nachenko http://drupal.org/node/194442 who has a module that deletes terms by vocabulary for Drupal 5.

I don't think that

lesmana@drupal.org - Fri, 2009-07-03 20:46

I don't think that hook_nodeapi is going to be able to do it in the way you're using it.

Let's say that at the time hook_nodeapi is called to execute your code, the terms haven't yet been removed from the node. At no point will taxonomy_term_count_nodes return 0 for any of those terms, because all of them will be still be assigned to the current node.

If instead you choose a stage at which the terms have been removed from the node, the removed terms won't be gathered by taxonomy_node_get_terms and won't be processed by your code.