Skip to main content
Category:

Code snippet that can use to delete users by role in drupal 8.

<?php

/**
 * hook_cron()
 */
function mymodule_cron()
{
  $role = 'editor';
  $ids = \Drupal::entityQuery('user')
    ->condition('roles', $role)
    ->execute();
  $users = \Drupal\user\Entity\User::loadMultiple($ids);
  foreach ($users as $user) {
    $user->delete();
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook