Scroll
Drupal 8 Delete entity programmatically
Drupal 8 Delete entity programmatically
Drupal 8 Delete entity programmatically
1 answer
votes: 1366
Answer
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();
}