Do you want to know how to Drupal?

Let's Drupal

How to run migration programmatically as a batch operations in Drupal 9?

It is quite easy to do instead of MigrateExecutable class, we need to use MigrateBatchExecutable and instead of import we need to use batchImport

use Drupal\migrate_tools\MigrateExecutable;


$migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
if (!empty($migration)) {
        // here we can set additional options like limit, update or force the import.
        $options = [
          'limit' => 0,
          'update' => 1,
          'force' => 1,
        ];

        $executable = new MigrateBatchExecutable($migration, new MigrateMessage(), $options);
        $executable->batchImport();
}