Jan 07, 2020 in Drupal 8, Snippets

Snippet on how to remove all users with a certain role in drupal. Easy to use in drush scripts, custom code, ...

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