Change record status: 
Project: 
Introduced in branch: 
11.2.x
Introduced in version: 
11.2.0
Description: 

To prevent duplicate batches from being created inadvertently in the same page request, you can use Drupal\Core\Batch\BatchBuilder::registerSetId() and Drupal\Core\Batch\BatchBuilder:isSetIdRegistered() to check and see if this batch has been built before.

Example:

use Drupal\Core\Batch\BatchBuilder;

...

if (!BatchBuilder::isSetIdRegistered('my_unique_id')) {
  $batch_builder = (new BatchBuilder())
    ->registerSetId('my_unique_id')
    ->setTitle(t('Batch Title'))
    ->setFinishCallback('batch_example_finished_callback')
    ->setInitMessage(t('The initialization message (optional)'));
  foreach ($ids as $id) {
    $batch_builder->addOperation('batch_example_callback', [$id]);
  }
  batch_set($batch_builder->toArray());
}
Impacts: 
Module developers