Conteúdo principal

Delete all terms from vocabulary in Drupal 10

With this code you can eliminate all record items of a key type in a taxonomy. in MY_VOCABULARY_ID. Put the name of your security machine.

<?php

use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Entity\EntityTypeManagerInterface;

$vid = 'MY_VOCABULARY_ID';

$entityTypeManager = \Drupal::service('entity_type.manager');
$termStorage = $entityTypeManager->getStorage('taxonomy_term');

$query = $termStorage->getQuery()
  ->condition('vid', $vid)
  ->accessCheck(FALSE);

$tids = $query->execute();

if (empty($tids)) {
  return;
}

$terms = $termStorage->loadMultiple($tids);
$termStorage->delete($terms);

?>
Section: