Skip to content

Instantly share code, notes, and snippets.

@krschmidt
Last active August 29, 2015 14:10
Show Gist options
  • Save krschmidt/540f1de3c375bd2f5226 to your computer and use it in GitHub Desktop.
Save krschmidt/540f1de3c375bd2f5226 to your computer and use it in GitHub Desktop.
Drush script to delete a taxonomy term and its children, as well as all nodes with that term
<?php
//Enter the taxonomy term ID you want to delete
$termid = -1;
$allterms = taxonomy_get_children($termid);
$allterms[] = $termid;
echo "Deleting $termid and its children\n";
echo "Deleting " . count($allterms) . " terms\n";
foreach($allterms as $term) {
$term = taxonomy_term_load($term);
$nodes = taxonomy_select_nodes($term->tid, false);
echo $term->name . " has " . count($nodes) . " children to delete\n";
$successful_delete = true;
try{
node_delete_multiple($nodes);
}
catch(Exception $e) {
echo "Error deleting nodes in " . $term->name . ", check manually\n";
$successful_delete = false;
}
if($successful_delete) {
taxonomy_term_delete($term->tid);
echo "Deleted taxonomy term " . $term->name . "\n";
}
}
taxonomy_term_delete($termid);
echo "Parent term deleted. All set!\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment