Beginner’s Guide to Drupal 10 Batch Process with Easy Examples

Drupal 10 Batch Process: When working with large amounts of data in Drupal 10, running everything at once can make your site slow or even cause a PHP timeout. To solve this, Drupal provides the Batch API.

The Batch Process in Drupal 10 is almost the same as Drupal 8 and 9, so if you learned it before, it will still work here.

In this guide, you’ll learn how to set up and run a Batch Process in Drupal 10 step by step.

What is Batch Processing in Drupal?

Batch processing lets you:

  • Split big tasks into smaller parts (chunks).
  • Avoid server timeouts.
  • Show a progress bar to users while tasks run.

Example: Updating 50,000 nodes → instead of processing all at once, Batch API processes them in groups of 100.

Structure of a Batch Process

A Drupal batch process has three main parts:

  1. Operations → Define the repeated task.
  2. Finished Callback → What happens after the batch ends.
  3. Batch Setup → Register and run the batch.

Example: Updating Node Titles

Here’s a working example of a Drupal 10 Batch Process:

Step 1: Define an operation

function mymodule_batch_operation($nid, array &$context) {
  $node = \Drupal\node\Entity\Node::load($nid);
  if ($node) {
    $node->setTitle('Updated Title - ' . $nid);
    $node->save();
  }
  $context['message'] = t('Processing node @nid', ['@nid' => $nid]);
}

Step 2: Define the finished callback

function mymodule_batch_finished($success, $results, $operations) {
  if ($success) {
    \Drupal::messenger()->addStatus(t('Batch processing finished successfully.'));
  } else {
    \Drupal::messenger()->addError(t('Batch processing encountered an error.'));
  }
}

Step 3: Setup and start the batch

function mymodule_start_batch() {
  $nids = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->execute();

  $operations = [];
  foreach ($nids as $nid) {
    $operations[] = ['mymodule_batch_operation', [$nid]];
  }

  $batch = [
    'title' => t('Updating nodes...'),
    'operations' => $operations,
    'finished' => 'mymodule_batch_finished',
  ];

  batch_set($batch);
}

Step 4: Run the batch from a route or form

For example, from a route callback:

function mymodule_batch_page() {
  mymodule_start_batch();
  return batch_process(\Drupal::url('<front>'));
}

When you visit the route, Drupal will show a progress bar and update nodes in batches.

Best Practices

Keep batch operations small (process fewer items at a time).
Use try/catch inside operations to handle errors safely.
Show clear progress messages to users.
Don’t forget to test on a smaller dataset before running on production.

The Batch API in Drupal 10 is the best way to handle large data operations without timeouts. Whether you are migrating data, updating thousands of nodes, or performing heavy tasks, the Batch Process makes it safe and user-friendly.

Now you can confidently use Batch Processing in Drupal 10 to speed up and stabilize your projects.