Looking for a module that deletes orphan free tags.

Events happening in the community are now at Drupal community events on www.drupal.org.
ki's picture

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.

Comments

It wouldn't take much

PixelClever's picture

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.

Drupal theming, Module development and logo design:
http://www.PixelClever.com

Clean Taxonomy

gambling's picture

This module exists now, enables a total clean-up of empty and orphaned terms.

Clean Taxonomy Demo

Download from Clean Taxonomy Module

Taxonomy manager

bonobo's picture

Taxonomy manager:

http://drupal.org/project/taxonomy_manager

Cheers,

Bill


FunnyMonkey
Click. Connect. Learn.

How?

szy's picture

Could you specify how Taxonomy Manager deletes orphan tags?

Szy.

in D6 Taxonomy Manager?

matt v.'s picture

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

highermath's picture

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.

Re: Simple to do with SQL

cackalacky's picture

The site got shuffled around a bit - that article is now at: http://michaelosmith.com/node/8

As SQL in a Drupal db_query function

mlncn's picture

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

benjamin, agaric

Here's a little module,

sapark's picture

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-gdo's picture

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.

Also: Views Bulk Operations

itangalo's picture

If you're still looking for a solution for this, I guess Views Bulk Operations would do the trick.
If you can set up a view showing all orphaned terms it should be a breeze to get Views Bulk Operations to delete them.

Best of luck!

//Johan Falk

Ny med Drupal? Kolla in sidan med tips och guider!
Har du fått problem? Kolla in FAQ-sidan med besvarade frågor.

wwwoliondorcom's picture

hi,

I was looking for the same thing, do you know how to automatically delete terms without a node ?

Is there a new module to do that now ?

Thanks a lot.

Probably a better way is to

lesmana@drupal.org-gdo's picture

Probably a better way is to use hook_cron() to go through a vocabulary and delete unused terms. Here's the code you would put into a module named "tagremover":

<?php
function tagremover_cron() {
 
$vid = 4// change this to the VID number of the vocabulary you wish to scrub
 
$terms = taxonomy_get_children($tid = 0, $vid);
  
// 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);
      }
   }
}
?>

EDIT: forgot to put a semi-colon in at the end of line 3.

Delete orphans tags

wwwoliondorcom's picture

Thank you, but can you explain me how to use this code ?

I put it in a text file and then ?

Sorry, but i never did that kind of thing, i come from Joomla ;-)

You have to make it into a

lesmana@drupal.org-gdo's picture

You have to make it into a module. A module consists of at least a .info file and a .module file, inside a directory, and the whole thing placed in your contributed modules folder (sites/all/modules if you're not sure).

The .info file is a text file and contains a few necessary lines that describe the module to Drupal. Here's what your tagremover.info file will look like:

name = Tag Remover
description = "Removes unused tags of the specified vocabulary on each cron run."
core = 6.x
version = "6.x"

You would put the code from my earlier post in a file called tagremover.module, making sure to change the $vid to the vocabulary ID of the vocabulary you want to scrub. If you're not sure what the VID is, go to the taxonomy admin page and click the link to edit the vocabulary you want to be checking. The VID is the number in the path.

Once you have your tagremover.info and tagremover.module files, place them both in a directory called "sites/all/modules/tagremover".

Go to the Modules page on your Drupal site, enable the Tag Remover module, and run cron. Your tag removing code should run.

GREAT !

wwwoliondorcom's picture

Thanks you so much for all your explanation.
I will try it, but please can you confirm that I can write:

taxonomy_term_count_nodes($term->tid) <= 1

instead of: taxonomy_term_count_nodes($term->tid) == 0

to also delete terms used by only 1 node ?

(i guess it will help to clean my too large terms base)

Yes, that would work, but

lesmana@drupal.org-gdo's picture

Yes, that would work, but keep in mind that this code will run every time cron runs. It sounds like you want to run this code only once. If that's true, you could install this module, run cron, then change the code back or uninstall the module. I don't think you'll want to regularly get rid of terms that are being used, even if they are only used once at that moment.

wwwoliondorcom's picture

Me again ;-)

Sorry but I get this error:

Parse error: syntax error, unexpected T_VARIABLE in /sites/all/modules/tagremover/tagremover.module on line 4

Line 4 is: $terms = taxonomy_get_children($tid = 0, $vid);

I don't know if it could be the cause of the problem but i am using this module with drupal 5.19

Thanks for all your help !

There should be a semi-colon

lesmana@drupal.org-gdo's picture

There should be a semi-colon (;) at the end of the line 3, before the comment. I accidentally left it out of the above post.

It works

wwwoliondorcom's picture

Thanks, it works now, I managed to delete some terms.

Now i need to fix another problem that freezes cron (it seems that memory usage is too high on my site and cron cannot finish everytime it starts).

Or maybe I really have too many terms for drupal to handle them (65000 terms in the term_data base)

I will report if i can find a solution.

Thank you again.

new Delete Orphaned Terms module

matt v.'s picture

A while ago, I created a module based on the snippets above from sapark and lesmana. Since there has been renewed interest in this thread, I decided to finally clean up the module a bit and post it. I'll try to add a README.txt file soon. In the mean time, be cautious and back up your database before using this module.

You can find the module via the Delete Orphaned Terms project page. A development snapshot release should be available within the next 12 hours or so, but the module is available via CVS now.

Thanks to everyone who contributed to the discussion.

Delete orphan terms in database ?

wwwoliondorcom's picture

Hello,

Sorry to be here again ;-)

but I still can't use your module properly because there is something wrong with my site and it never finishes any Cron.

Would it be difficult to use a command in Mysql database to delete all the orphans terms ?

Thanks again.

Cron isn't necessary

matt v.'s picture

The Delete Orphaned Terms module does not currently require cron in order to run. The orphaned terms are deleted immediately when you submit the form on the administration page for the module.

Tag remover for Drupal 5

wwwoliondorcom's picture

Oh sorry, I was speaking about Tag Remover.

Because your module doesn't work with drupal 5 and my site is D5.

Thanks.

The right answer is to fix

lesmana@drupal.org-gdo's picture

The right answer is to fix your cron problem, but for a quick solution you could call the function tagremover_cron() from a node that has the PHP code input filter applied to it.

wwwoliondorcom's picture

Hi,

Finally I managed to delete 40000 terms thanks to TAG REMOVER on my Drupal 5 site, thank you again.

I guess that since now I will regularly clean the tags as my community website now looks more "healthy" and google bots will have less tags pages to browse.

But I am wondering, between two cleanings, do you know a way to add the orphans terms to a node that would say "You can add your content to this page" ?

I think it could be interesting to explain people that they can post on the site, as google often sends visitors to orphans tags pages.

Would it be difficult to add the orphans tags to a page inviting people to post ?

Thanks for all your help.

I found an additional way

giorgio79's picture

I found an additional way :)

http://drupal.org/node/537418

Install VBO, term node count, filter for terms with 0 nodes, and delete them :)

Review Critical
ClipGlobe - World Travel

****Me and Drupal :)****
Clickbank IPN - Sell online or create a membership site with the largest affiliate network!
Review Critical - One of my sites

Delete orphan terms is good and easy to use.

MakeOnlineShop's picture

Delete orphan terms is good and easy to use.

Thanks.

tag remover for drupal 7

mvdve's picture

I did a rewrite of the tag remover snippet for drupal 7.
Maybe handy as there is no module for this purpose (i am probably going to build a simple one from this code.

<?php
function tagremove_cron() {
  
$vid = 3;               //select taxomony vocabulary
  
$parent = 0;            //select the parent term, 0 = all
 
$max_depth = 1;         //select the maximum depth to get
 
$load_entities = FALSE; //Load a full entity or not (full entity is not recommended).
 
  
$terms = taxonomy_get_tree($vid, $parent, $max_depth, $load_entities);

    foreach (
$terms as $term) {
       
$tid = $term->tid;   //select tid from terms array
     
$pager = FALSE;     //set pager to false in taxonomy select function
      
$limit = FALSE;     // set limit to false in taxonomy select function

    
$nids = taxonomy_select_nodes($tid, $pager, $limit);

      if(empty(
$nids)) {
        
taxonomy_term_delete($term->tid);
       }
  }
}
?>