You are here

function delete_all_users_batch_delete in Delete all 2.x

Same name and namespace in other branches
  1. 8 includes/delete_all.user.batch.inc \delete_all_users_batch_delete()

Function to delete users using Batch API.

Parameters

array $users: Array of users to delete.

array &$context: Sandbox context array.

1 string reference to 'delete_all_users_batch_delete'
UserDeleteController::getUserDeleteBatch in src/Controller/UserDeleteController.php

File

includes/delete_all.user.batch.inc, line 11

Code

function delete_all_users_batch_delete($users = FALSE, &$context) {
  $db = \Drupal::database();
  $entity_type_manager = \Drupal::entityTypeManager();
  if ($users === FALSE) {
    if (!isset($context['sandbox']['progress'])) {
      $context['sandbox']['progress'] = 0;
      $context['sandbox']['current_uid'] = 1;
      $context['sandbox']['max'] = $db
        ->select('users', 'u')
        ->fields('u')
        ->condition('uid', 1, '>')
        ->countQuery()
        ->execute()
        ->fetchField();

      // Collect results to process in the finished callback.
      $context['results']['user_count'] = $context['sandbox']['max'];
    }

    // Get a batch of 50 users from all users to delete.
    $users_to_delete = $db
      ->select('users', 'u')
      ->fields('u', [
      'uid',
    ])
      ->condition('uid', $context['sandbox']['current_uid'], '>')
      ->range(0, 50)
      ->execute()
      ->fetchCol();
  }
  else {
    if (!isset($context['sandbox']['progress'])) {
      $context['sandbox']['progress'] = 0;
      $context['sandbox']['max'] = count($users);

      // Collect results to process in the finished callback.
      $context['results']['user_count'] = $context['sandbox']['max'];
    }

    // Get a batch of 50 users from the list of users to delete.
    $users_to_delete = array_slice($users, $context['sandbox']['progress'], 50);
  }
  if ($context['sandbox']['max'] + 1 > 0) {
    if (!empty($users_to_delete)) {
      foreach ($users_to_delete as $uid) {
        $account = $entity_type_manager
          ->getStorage('user')
          ->load($uid);

        // Delete user.
        $entity_type_manager
          ->getStorage('user')
          ->load($uid)
          ->delete();
        $context['message'] = t('Deleting user with uid %uid', [
          '%uid' => $uid,
        ]);
        $context['sandbox']['current_uid'] = $uid;
        $context['sandbox']['progress']++;
      }
    }

    // Inform the batch engine that we are not finished,
    // and provide an estimation of the completion level we reached.
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
}