How to write big updation code using Batch API? I am writing a script to update all website registered user's Username &amp; Email address. I have more then 5000+ users in my website which causes PHP execution time out error.

So I tried to implement Batch API but I am unable to implement it. I passed whole user list to Batch API and perform operations on it. But Batch API also shows the same PHP time out error.

Can someone help me to implement Batch API?

Here is my code:

function anonymize_data_menu() {
  $items = array();
  $items['admin/config/content/anonymize-user-data'] = array(
    'title' =&gt; 'Anonymize User Data',
    'description' =&gt; 'Click this link to anonymize user data',
    'type' =&gt; MENU_LOCAL_ACTION,
    'page callback' =&gt; '_anonymize_users_data',
    'access arguments' =&gt; array('administer user anonymize'),
    'file' =&gt; 'anonymize_data.user.inc',
  ); 
  return $items; 
}

function _anonymize_users_data() {
  $users = entity_load('user');
  $chunks = array_chunk($users, 20);
  $count_user = count($chunks);

  foreach ($chunks as $chunk) {
     $operations[] =  array('user_anonymize_op', array($chunk));
      //user_anonymize_op($chunk);
     //krumo($chunk);
  }
  $batch = array(
    'operations' =&gt; $operations,
    'title' =&gt; t('Import batch'),
    'init_message' =&gt; t('Initializing'),
    'error_message' =&gt; t('An error occurred'),
    'finished' =&gt; 'user_anonymize_op_finished'
  );
  batch_set($batch);
  batch_process('admin/people');
}

function user_anonymize_op($users, &amp;$context) {
  foreach ($users as $user) {
    # code...
    //krumo($user);
   $context['message'] = t('processing user @user', array('@user' =&gt; $user-&gt;name));
    //
      if($user-&gt;uid != 0 &amp;&amp; $user-&gt;uid != 1) {
      $user-&gt;name = $i . '-' . $user-&gt;name;
      $user-&gt;pass = user_hash_password('blah');
      user_save($user);
    }
  }
}

function user_anonymize_op_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  drupal_set_message(t($message));
}


I am very confused about that how to process chunk of users.

Can someone help me it will be great.
The timeout is caused because you're trying to load all 5K+ users before you start the batch. Instead get the User IDs first and load when processing. Check an example below. Also have a look at Batch API Overview documentation page, before you continue:

function anonymize_data_menu() {
  $items = array();
  $items['admin/config/content/anonymize-user-data'] = array(
    'title' =&gt; 'Anonymize User Data',
    'description' =&gt; 'Click this link to anonymize user data',
    'type' =&gt; MENU_LOCAL_ACTION,
    'page callback' =&gt; '_anonymize_users_data',
    'access arguments' =&gt; array('administer user anonymize'),
    'file' =&gt; 'anonymize_data.user.inc',
  ); 
  return $items; 
}

function _anonymize_users_data() {
  // Instead of loading all users at once,
  // first get there uid and then load chunk of user while processing.
  // $users = entity_load('user');
  $uids = db_select('users', 'u')
    -&gt;condition('u.uid', 1, '&gt;')
    -&gt;fields('u', array('uid'))
    -&gt;execute()-&gt;fetchAllKeyed(0, 0);

  $chunk_count = 20;
  // We've all rows here, split them in chunks.
  $chunks = array_chunk($uids, $chunk_count, TRUE);
  // Get all rows count.
  $num_operations = count($uids);

  $operations = array();
  foreach ($chunks as $chunk) {
    $operations[] =  array('user_anonymize_op', array(
      $chunk,
      array(
        'max' =&gt; $num_operations,
        'chunk' =&gt; $chunk_count,
      )
    ));
  }

  $batch = array(
    'operations' =&gt; $operations,
    'title' =&gt; t('Import batch'),
    'init_message' =&gt; t('Initializing'),
    'error_message' =&gt; t('An error occurred'),
    'finished' =&gt; 'user_anonymize_op_finished'
  );

  batch_set($batch);
  batch_process('admin/people');
}

function user_anonymize_op($rows, $batch_data, &amp;$context) {
  // Define variables on start of batch operation.
  if (!isset($context['results']['progress'])) {
    $context['results']['progress'] = 0;
    $context['results']['chunk'] = $batch_data['chunk'];
    $context['results']['max'] = $batch_data['max'];
    $context['results']['failed'] = array();
  }

  // Do your processing here. START HERE.
  $names = [];
  $users = entity_load('user', $rows);
  foreach ($users as $account) {
    if (!empty($account-&gt;uid) &amp;&amp; ($account-&gt;uid &gt; 1)) {
      // What is $i here?
      $account-&gt;name = $i . '-' . $account-&gt;name;
      $account-&gt;pass = user_hash_password('blah');
      user_save($account);
      $names[] = $account-&gt;name;
    }
  }
  // Do your processing here. END HERE.

  // Update completion count.
  $context['results']['progress'] = $context['results']['progress'] + count($rows);

  $context['message'] = t('Processed @name user data.', array('@name' =&gt; implode(', ', $names)));

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

function user_anonymize_op_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  drupal_set_message(t($message));
}