I am inserting and updating node programmatically in drupal 7.When am updating I want to delete the taxonomy terms tagged in that particular node and have to insert new terms.

Please help me to untag the taxonomy terms of that node.the unset($node->taxonomy_field) is not working.Can anyone help me?

  • Have you tried to just set it to NULL or empty array() ? – SebCorbin Sep 3 '13 at 12:51
  • You want to clear them from node/%/edit form or programatically unset them at some other point? Also, please post the code you are using, not just the one line, but a whole hook that is supposed to do the job, so we know when you wanted it to fire and could get the idea of how it works. – Mołot Nov 18 '13 at 9:34

With Views Bulk Operations for Drupal 7, you can execute arbitrary PHP scripts.

In order to bulk-delete a specific taxonomy term, you can make a View that shows all content with the particular term you want to remove: Be sure to add a Relationship in the View like "Content: Taxonomy terms on node" so you can filter out content with the terms you want to keep.

Then in the same View, you'd add a VBO field to execute an arbitrary PHP script. VBO no longer uses $node but rather $entity, so you can pass through the following script modified from Dave Parrish's answer above (where field_name_of_taxonomy_term would be the name of the Term Reference or Entity Reference field where you save the taxonomy terms on the node):

for ($i = 0; count($entity->field_name_of_taxonomy_term[$entity->language]) > $i; $i++) {
    if ($entity->field_name_of_taxonomy_term[$entity->language][$i]['tid'] == 'TID to Remove') {

unset should do the trick. In my case, my taxonomy field was called field_genre and here is how I removed a taxonomy from a node.

for ($i = 0; count($node->field_genre[$node->language]) > $i; $i++) {
    if ($node->field_genre[$node->language][$i]['tid'] == $tid_to_remove) {
  • 1
    It might be helpful to indicate where you've placed your code. – othermachines Aug 16 '15 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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