2

The task is to delete the entities, which satisfies some specified conditions. How can I do it ?

$current_user = \Drupal::currentUser()->id();
$storage = \Drupal::entityManager()->getStorage('context');

$query = $storage->getQuery()->condition('user_id', $current_user);

$query = $storage->getQuery()->delete();
$query->condition('user_id', $current_user);
$query->condition('key_text', $key);

$query->execute();

But the code returns: Fatal error: Call to undefined method Drupal\Core\Config\Entity\Query\Query::delete()

badm
  • 219
  • 2
  • 13

4 Answers4

4

To query entities you can use entityQuery, example below uses this.

// Get all users with email containing "xyz"
$query = \Drupal::entityQuery('user')
  ->condition('mail', "XYZ", 'CONTAINS');
$uids = $query->execute();

// Load these entities ($uids) in our case using storage controller.
// We call loadMultiple method and give $uids array as argument.     
$itemsToDelete = \Drupal::entityTypeManager()->getStorage('user')
  ->loadMultiple($uids);

// Loop through our entities and deleting them by calling by delete method.
foreach ($itemsToDelete as $item) {
  $item->delete();
}
  • 2
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 03 '17 at 12:55
2

Use the entity storage's delete() method to delete multiple entities. There's no need to iterate over all of them.

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

$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($tids);
$storage_handler->delete($entities);

Taken from https://stackoverflow.com/a/43786945/442022.

colan
  • 2,818
  • 2
  • 20
  • 17
  • 2
    you would run out of memory if there are a lot of entities (same thing with the accepted answer). Sometimes you just need to load and delete one by one. – Dmitry Boychev Oct 24 '20 at 21:41
0

You need to get your entities first before you can actually delete them. This method is also required for updating them or getting information from them. Hope the code below helps out :)

$current_user = \Drupal::currentUser()->id();    

$ids = \Drupal::entityQuery('context')
          ->condition('user_id', $current_user)
          ->execute();

$itemsToDelete = $this->entityTypeManager->getStorage('context')
          ->loadMultiple($ids);


foreach ($itemsToDelete as $item) {
   $item->delete();
}
VJamie
  • 616
  • 3
  • 14
0

The error message is caused from the fact the class you are using for querying your configuration entity doesn't have a delete() method. This is also true for content entities. The delete() method is implemented by the entity, so the correct code is similar to the following one.

$storage = \Drupal::entityTypeManager()->getStorage('context');
$query = $storage->getQuery();

$ids = $query->condition('user_id', $current_user)
  ->condition('key_text', $key)
  ->execute();

foreach ($storage->loadMultiple($ids) as $entity) {
  $entity->delete();
}

See entity.query service deprecated in favor of EntityStorageInterface::getQuery().

apaderno
  • 28,547
  • 16
  • 75
  • 90