3

In Drupal 7 there was taxonomy_vocabulary_delete($vid), but this doesn't seem to be there in Drupal 8.

What's the equivalent code for Drupal 8?

3 Answers 3

6

I believe I have figured this out. You have to load the vocabulary using the Vocabulary entity class, then call the delete method on it. So something like this:

Vocabulary::load($vid)->delete();

2
  • But how do you delete by vocabulary name? Commented Aug 4, 2016 at 18:27
  • 1
    In Drupal 8, the vocabulary ID is the machine name of the vocabulary. The shown code is doing already that.
    – avpaderno
    Commented Aug 31, 2016 at 8:41
1
  $tids = \Drupal::entityQuery('taxonomy_term')
    ->condition('vid', 'tags')
    ->execute();

  $controller = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $entities = $controller->loadMultiple($tids);
  $controller->delete($entities);

Or

     $vid = 'tags';
      $tids = Drupal::entityQuery('taxonomy_term')
          ->condition('vid', $vid)
          ->execute();
      entity_delete_multiple('taxonomy_term', $tids);
0

Here is how I did it

$vocabularies = [
'test',
'second_test'
];
foreach ($vocabularies as $vocabulary) {
    $vocab = Drupal\taxonomy\Entity\Vocabulary::load($vocabulary);
    if ($vocab) {
        $vocab->delete();
    }
}

Your Answer

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

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