2

I want to delete all taxonomy terms of particular vocabulary using entity_delete_multiple. For node it is working fine

$result = \Drupal::entityQuery('node')
      ->condition('type', 'page')
      ->execute();
  entity_delete_multiple('node', $result);

If i am changing taxonomy_term and type to tags or any other vocabulary, or replaced type by bundle

$result = \Drupal::entityQuery('taxonomy_term')
      ->condition('type', 'tags')
      ->execute();
  entity_delete_multiple('taxonomy_term', $result);

it is throwing error like this.

Drupal\Core\Entity\Query\QueryException: 'type' not found in Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable() (line 304 of core/lib/Drupal/Core/Entity/Query/Sql/Tables.php).

And also i need a same query to delete user(s), watchdog and profiles.

Is there any way to do it?

6

Try to use vid instead of type. I haven't tried the code, but try the following

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

Here is a good example for using entityQuery.

For deleting users, you can use entity_delete_multiple or user_delete_multiple

To delete user with uid 10

entity_delete_multiple('user', \Drupal::entityQuery('user')->condition('uid', '10', '=')->execute());

or pass an array of uids to user_delete_multiple

user_delete_multiple(array $uids);

I'm not aware of how to delete comments.

  • That's D7 documentation and not correct for 8.x – Berdir Aug 24 '16 at 7:22
  • @Berdir thanks for pointing it out, I have removed the link and edited my answer – Suresh R Aug 24 '16 at 7:28
  • @Suresh: thanks for your answer. And also is there any way to delete User(s), Profile and comments – Ajay Reddy Aug 24 '16 at 9:21
  • 1
    @AAjayKumarReddy I have updated my answer. I'm not aware of how to delete comments. – Suresh R Aug 24 '16 at 9:36
  • @Suresh: What about profile? this is also entity right.. – Ajay Reddy Aug 24 '16 at 9:57
3

For taxonomy use vid:

$result = \Drupal::entityQuery('taxonomy_term')
      ->condition('vid', 'tags')
      ->execute();

Which is the bundle of taxonomy.

2

For delete all taxonomy terms of particular vocabulary.

Try

 $vid = 'name_of_the_vocabulary';
      $tids = \Drupal::entityQuery('taxonomy_term')
          ->condition('vid', $vid)
          ->execute();
      entity_delete_multiple('taxonomy_term', $tids);
1

You can try below code,which works for me

 $vids = Vocabulary::loadMultiple();
    foreach ($vids as $vid) {
      if ($vid->label() == 'YourVocab') {
        $container = \Drupal::getContainer();
        $terms = $container->get('entity.manager')->getStorage('taxonomy_term')->loadTree($vid->id());
        if (!empty($terms)) {
          entity_delete_multiple('taxonomy_term', $terms);
        }
        break;
      }
}
1

I know the question specifically asks about entity_delete_multiple, but the function is deprecated, scheduled to be removed "before" Drupal 9. Here's a version without entity_delete_multiple

/**
 * Delete all taxonomy terms from a vocabulary
 * @param $vid
 */
function delete_terms_from_vocab($vid) {

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

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

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

  $controller->delete($entities);
}
  • Probably the best technique these days, but you probably want to rename $controller (which has a different meaning) to $storage. – FGM Jul 21 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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