Skip to content

Instantly share code, notes, and snippets.

@aschanMarcus
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save aschanMarcus/e903b3fa8df7ea6005c8 to your computer and use it in GitHub Desktop.

Select an option

Save aschanMarcus/e903b3fa8df7ea6005c8 to your computer and use it in GitHub Desktop.
Delete unused taxonomy terms
<?php
$field = 'VOCABULARY_MACHINE_NAME';
// Search for unused taxonomy terms.
$query = db_query('SELECT DISTINCT field_' . $field . '_tid FROM field_data_field_' . $field . ' WHERE field_' . $field . '_tid NOT IN (SELECT tid FROM taxonomy_term_data)');
$results = $query->fetchCol();
// Output results with devel.
dpm($results);
// Delete unused taxonomy terms.
foreach ($results as $result) {
db_delete('field_data_field_' . $field)
->condition('field_' . $field . '_tid', $result)
->execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment