How to Use Drush for Batch Process in Drupal 10: Easy Examples

Why Use Drush for Batch Process?

Can run on production servers safely.

No need to open the browser.

Ideal for long-running batch tasks.

Create a Custom Drush Command

In Drupal 10, you create Drush commands inside a module in the src/Commands folder.

Step 1: File Structure

modules/custom/mymodule/src/Commands/BatchCommands.php

Step 2: Define the Drush Command

<?php

namespace Drupal\mymodule\Commands;

use Drush\Commands\DrushCommands;

class BatchCommands extends DrushCommands {

  /**
   * Run batch to update nodes.
   *
   * @command mymodule:batch-update-nodes
   * @aliases bun
   */
  public function batchUpdateNodes() {
    // Start the batch process.
    \Drupal::service('mymodule.batch')->startBatch();
    $this->logger()->success(dt('Batch process started. Check progress in the browser.'));
  }

}

@command → defines the Drush command name.

@aliases → short version.

\Drupal::service('mymodule.batch')->startBatch() → calls a service that triggers your batch (we will define it next).

Step 3: Move Batch Logic into a Service

Create a service to handle the batch process:

<?php

namespace Drupal\mymodule;

use Drupal\Core\Batch\BatchBuilder;

class BatchService {

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

    $operations = [];
    foreach ($nids as $nid) {
      $operations[] = ['\Drupal\mymodule\BatchService::batchOperation', [$nid]];
    }

    $batch = [
      'title' => t('Updating nodes...'),
      'operations' => $operations,
      'finished' => ['\Drupal\mymodule\BatchService', 'batchFinished'],
    ];

    batch_set($batch);
    batch_process(); // Runs batch immediately
  }

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

  public static function batchFinished($success, $results, $operations) {
    if ($success) {
      \Drupal::logger('mymodule')->notice('Batch processing finished successfully.');
    } else {
      \Drupal::logger('mymodule')->error('Batch processing encountered an error.');
    }
  }

}

Run the Drush Batch Command

Once your module is enabled, run the command:

drush mymodule:batch-update-nodes
# Or using alias
drush bun
  • The batch will process all nodes without opening the browser.
  • Progress is logged in watchdog or Drupal logs.

Best Practices for Drush Batch

  • Always test with smaller datasets first.
  • Use try/catch in batchOperation() to avoid crashes.
  • Log messages for monitoring progress.
  • Combine with entity queues for very large datasets.

Conclusion

Using Drush commands with Batch API in Drupal 10 is a powerful way to process large data sets safely and efficiently.

  • Browser not needed.
  • Can run on production safely.
  • Works exactly like a normal batch but ideal for automated scripts or cron jobs.