logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

Drupal Delete entity programmatically

22/02/2025, by Anonymous (not verified)
Permalink

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();
}