1

I had a taxonomy vocabulary (Tags) shared across all of my content types including those like forum and blog that could be user created. I'd like to remove the terms / Tags from all node types except story. Story can only be created by site authors. Is there an acceptable way to do this?

I tried deleting the tag field from the content type. However, it does just that. The field isn't part of the type, but the terms remain assigned to the existing nodes.

If I were going to do this programmatically with a MySQL script, are there tables other than taxonomy_index, field_data_taxonomy_vocabulary_, and field_revision_taxonomy_vocabulary_ that I'd need to delete rows from? It seems like I could generate a list of nids I want to remove terms from, and then delete the corresponding rows from those 3 tables.

Thanks in advance for any help. I'd be happy to provide additional information.

3 Answers 3

2

The field isn't part of the type, but the terms remain assigned to the existing nodes.

That doesn't sound right; to the best of my knowledge removing the field from a content type will (normally) remove all data attached to nodes of that type. They get moved to a field_deleted_* table and then purged on a cron job.

It might indicate there's a problem somewhere in your installation, or it might just be 'one of those things'.

The 3 tables you mentioned are the only places you'll need to delete data from, but you'll also need to flush the caches after the purge (or at least truncate cache_field)

1
  • I appreciate your help. I ran cron and cleared my cache. There's no sign of a field_deleted_* table in my database. Odd. Is it possible things are a bit off because these terms were originally present in my D6 install, which was then upgraded to D7? When I go to /category/tags/TERM (my path to a term) the nodes still show up.
    – guinness74
    May 27, 2014 at 17:05
1

Deleting the taxonomy field from the content type didn't remove the taxonomy terms from the nodes. Therefore, I wrote a PHP script to generate MySQL code to programmatically delete the terms after removing the taxonomy field from the nodes via the admin menu.

<?php

// put your database name here
$database = '';

// put your vocabulary table suffix here
// will be appeneded to table names like `field_data_taxonomy_` and `field_revision_taxonomy_`
$vocabulary = 'vocabulary_2';

$connection = mysql_connect(HOST, USER, PASS);
$nids = array();
$fp = fopen('taxonomy_clean.sql', 'w');

if (!$connection)
{
    echo 'Unable to connect to database: ' . mysql_error();
    exit;
}

if (!mysql_select_db($database))
{
    echo 'Unable to select ' . $database . ': ' . mysql_error();
    exit;
}

// get nid from all nodes except story type
$result = mysql_query('select * from ' . $database . '.node where type != \'story\' order by nid asc;');

while ($row = mysql_fetch_assoc($result))
{
    $nid = $row['nid'];
    $nids[] = $nid;
}

echo PHP_EOL, count($nids), ' nids', PHP_EOL;

// remove the taxonomy terms from the nodes
$taxonomy_index = 'DELETE FROM ' . $database . '.taxonomy_index WHERE nid IN (' . implode(',', $nids) . ');';

$field_data_taxonomy_vocabulary = 'DELETE FROM ' . $database . '.field_data_taxonomy_' . $vocabulary . ' WHERE entity_id IN (' . implode(',', $nids) . ');';

$field_revision_taxonomy_vocabulary = 'DELETE FROM ' . $database . '.field_revision_taxonomy_' . $vocabulary . ' WHERE entity_id IN (' . implode(',', $nids) . ');';

// write the MySQL script to disk
fwrite($fp, $taxonomy_index . PHP_EOL . PHP_EOL);
fwrite($fp, $field_data_taxonomy_vocabulary . PHP_EOL . PHP_EOL);
fwrite($fp, $field_revision_taxonomy_vocabulary . PHP_EOL . PHP_EOL);

fclose($fp);

?>

The script worked fine with PHP 5.4, and will generate taxonomy_clean.sql. I used mysql via the command line to run taxonomy_clean.sql. I cleared the cache after and everything worked fine.

0

You can do it using Drush field-delete,

Delete a field and its instances.

Examples:

 field-delete city                         Delete the city field and any instances it might have. 
 field-delete city --bundle=article        Delete the city instance on the article bundle         


Arguments:
 field_name                                Name of field to delete. 


Options:
 --bundle                                  Only delete the instance attached to this bundle. If omitted, admin can choose to delete one instance or whole 
                                           field.                                                                                                         
 --entity_type                             Disambiguate a particular bundle from identically named bundles. Usually not needed.                           

Original Source

Your Answer

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

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