Skip to content

Instantly share code, notes, and snippets.

@cristiroma
Last active November 14, 2021 09:29
Show Gist options
  • Save cristiroma/ca742687cecbbb6015317b6868bc016b to your computer and use it in GitHub Desktop.
Save cristiroma/ca742687cecbbb6015317b6868bc016b to your computer and use it in GitHub Desktop.
Drush script to delete a subtree of a taxonomy in Drupal 8+
<?php
/* Root term not deleted */
$exit = 0;
$vid = $extra[0];
if (empty($vid)) {
$exit = 1;
}
$parent = $extra[1];
if (empty($parent)) {
$exit = 1;
}
if ($exit) {
echo "\n\nUsage:./vendor/bin/drush scr scripts/delete-taxonomy-subtree.php -- <vid> <parent_tid>\n\n";
exit -2;
}
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $parent, NULL, TRUE);
/** @var \Drupal\taxonomy\Entity\Term $term */
foreach($terms as $term) {
$label = $term->label();
echo "Deleting: $label\n";
$term->delete();
}
echo "Done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment