Last active
November 14, 2021 09:29
-
-
Save cristiroma/ca742687cecbbb6015317b6868bc016b to your computer and use it in GitHub Desktop.
Drush script to delete a subtree of a taxonomy in Drupal 8+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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