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

 


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.