I have a vocabulary with many terms that are duplicates. How can I remove those terms?

I have many duplicates; I need a way to delete them that is not deleting them one by one.

3 Answers 3


Run this code once in hook_init or custom php block or using drush


$vid = 1; // Replace 1 by vocabulary id for which you want to remove duplicates.

$terms = taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE);

// Loop through all terms in vocabulary
foreach ($terms as $term) {
  // Get all matching terms by term name
  $matching_terms = taxonomy_get_term_by_name($term->name);
  foreach ($matching_terms as $matching_term) {
    /* Check matching term belongs to vocabulary we are searching and matching term should not be exact term that first found so that duplicate can be removed */
    if ($matching_term->vid == $vid && $matching_term->tid != $term->tid) {
      // Do Term Delete


Important Note: Take Database backup before running above code, so that you can restore if something goes wrong !

  • 1
    Its worth noting that this deletes terms without updating or checking references to other nodes. Its very dangerous.
    – Christian
    Oct 27, 2015 at 4:08

The Term Merge module could help you.

When using taxonomy for free tagging purposes, it's easy to end up with several terms having the same meaning. This may be due to spelling errors, or different users simply making up synonymous terms as they go.

You, as an administrator, may then want to correct such errors or unify synonymous terms, thereby pruning the taxonomy to a more manageable set. This module allows you to merge multiple terms into one, while updating all fields referring to those terms to refer to the replacement term instead.

  • This is definitely the best option.
    – Christian
    Oct 27, 2015 at 4:07

You can do this using the Rules, To do this:

After updating existing content

I chose after updating a content so you can chose all nodes and select update, which can be unpublish/publish - update url alias, any will work.

Content is of type 

and enter your content type

Add loop

Make sure the loop loops through your tags, my tags machine-name is "field-basic-tags" so you would chose something like "node:field-basic-tags" in the loop Data selector.

then you would add an action to that loop,

Add an item to a list

Chose "list-item:parent" from the data-selector

And check Enforce uniqueness box and maybe you can also add,

Show a message on the site

to make sure your code worked.

here is an export of the rule.

    { "rules_enforce_uniqness_tag" : {
    "LABEL" : "Enforce Uniqness Tag",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_update" ],
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "page" : "page" } } } }
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-basic-tags" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "list_add" : {
                "list" : [ "list-item:parent" ],
                "item" : [ "list-item" ],
                "unique" : 1
      { "drupal_message" : { "message" : "Done" } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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