Nov 07, 2019 in Drupal 8, Snippets

Simple snippet on how to delete all users of a given role in drupal 8.

use  \Drupal\user\Entity\User;

$role = 'webmaster';
$ids = \Drupal::entityQuery('user')
      ->condition('status', 1)
      ->condition('roles', $role)
      ->execute();
    $users = User::loadMultiple($ids);
    foreach($users as $user) {
      $user->delete();
    }