Can a batch process be stopped Is there a way to force a batch process to stop if some kind of error occurs?  In particular, I have a batch that starts with saving a record to a database. Each subsequent batch operation depends on the database insert working correctly.  I'd like to force the batch to stop if the first batch fails.

The batch is called like this

$batch = array(
    'operations' =&gt; $operations,
    'finished' =&gt; 'mymodule_batch_finished',
    'title' =&gt; t('Merging records'),
    'init_message' =&gt; t('Starting processing'),
    'progress_message' =&gt; t('Processed @current out of @total.'),
    'error_message' =&gt; t('Processing has encountered an error'),
    'file' =&gt; drupal_get_path('module', 'mymodule') . '/forms/mymodule.inc'
);


where $operations is the array of functions to call.

Each function uses PDO to update the database.  I'm trying to figure out how to stop the subsequent $operations from being called if the query in the current operation fails.

try {
  $sth = $dbh-&gt;prepare($sql);
  $sth-&gt;bindParam(':newid',$new_id,PDO::PARAM_INT);
  $sth-&gt;bindParam(':id',$id,PDO::PARAM_INT);
  if($sth-&gt;execute()) {
    $context['results']['[] = $sth-&gt;rowCount();
  }
  else {
    // TODO: stop processing due to an error
  }
}
catch(PDOException $e) {
  // TODO: stop processing due to an error
}


If an error occurs, I want to bypass the remaining operations and go straight to mymodule_batch_finished
Inside your batch callback, where you do the batch work, you should be able to set $context['finished'] to 1, and that will signal to Drupal that the batch is complete.

So, in your case:

$error = FALSE;
try {
  $sth = $dbh-&gt;prepare($sql);
  $sth-&gt;bindParam(':newid',$new_id,PDO::PARAM_INT);
  $sth-&gt;bindParam(':id',$id,PDO::PARAM_INT);
  if($sth-&gt;execute()) {
    $context['results'][] = $sth-&gt;rowCount();
  }
  else {
    $error = TRUE;
  }
}
catch (PDOException $e) {
  $error = TRUE;
}
// If there was an error, immediately complete the batch process.
if ($error) {
  $context['finished'] = 1;
}


And, even better, you can pass in an error message in the $context as well, which you can check for in your finished callback, then display to the end user in case of failure.

If you want to immediately stop processing anything in other callbacks in the $operations list, you might also need to check for a static variable at the beginning of each callback, and skip the callback if it's in an error condition. You could try using variable_get/variable_set, or some other mechanism that persists between page requests. This would definitely be a hackish/last-resort solution, though!

See also:


How does the batch API work internally?
Batch operations API documentation