{"id":"4cfe6cd825475abf","repo":"mongodb/node-mongodb-native","slug":"this-batch-has-already-been-executed-create-new-b","errorCode":null,"errorMessage":"This batch has already been executed, create new batch to execute","messagePattern":"This batch has already been executed, create new batch to execute","errorType":"exception","errorClass":"MongoBatchReExecutionError","httpStatus":null,"severity":"error","filePath":"src/bulk/common.ts","lineNumber":1183,"sourceCode":"  get writeConcern(): WriteConcern | undefined {\n    return this.s.writeConcern;\n  }\n\n  get batches(): Batch[] {\n    const batches = [...this.s.batches];\n    if (this.isOrdered) {\n      if (this.s.currentBatch) batches.push(this.s.currentBatch);\n    } else {\n      if (this.s.currentInsertBatch) batches.push(this.s.currentInsertBatch);\n      if (this.s.currentUpdateBatch) batches.push(this.s.currentUpdateBatch);\n      if (this.s.currentRemoveBatch) batches.push(this.s.currentRemoveBatch);\n    }\n    return batches;\n  }\n\n  async execute(options: BulkWriteOptions = {}): Promise<BulkWriteResult> {\n    if (this.s.executed) {\n      throw new MongoBatchReExecutionError();\n    }\n\n    const writeConcern = WriteConcern.fromOptions(options);\n    if (writeConcern) {\n      this.s.writeConcern = writeConcern;\n    }\n\n    // If we have current batch\n    if (this.isOrdered) {\n      if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);\n    } else {\n      if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);\n      if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);\n      if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);\n    }\n    // If we have no operations in the bulk raise an error\n    if (this.s.batches.length === 0) {\n      throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty');","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/bulk/common.ts#L1165-L1201","documentation":"Thrown as MongoBatchReExecutionError when BulkOperationBase.execute() is called on a bulk operation that has already been executed (this.s.executed is true). A BulkOperationBase instance is single-use; once execute() runs, the internal batches are consumed and the instance cannot be reused.","triggerScenarios":"Calling await bulk.execute() twice on the same bulk op object. Holding a bulk op in a long-lived variable and calling execute() in a retry loop without creating a fresh instance.","commonSituations":"Retry logic that re-invokes execute() on failure; reusing a bulk op across request handlers; caching a bulk op instance.","solutions":["Create a fresh bulk op via collection.initializeOrderedBulkOp() / initializeUnorderedBulkOp() before each execute().","In retry logic, rebuild the operation list inside the retry block, not outside it.","Use collection.bulkWrite(ops) for one-shot calls; the driver creates a new internal op each time."],"exampleFix":"// before\nconst bulk = coll.initializeOrderedBulkOp();\nbulk.find({_id:1}).updateOne({$set:{a:1}});\nawait bulk.execute();\nawait bulk.execute(); // throws\n\n// after\nasync function run() {\n  const bulk = coll.initializeOrderedBulkOp();\n  bulk.find({_id:1}).updateOne({$set:{a:1}});\n  return bulk.execute();\n}","handlingStrategy":"validation","validationCode":"function assertNotExecuted(bulk) {\n  if (bulk.s.executed) {\n    throw new Error('bulk op already executed; create a new one');\n  }\n}","typeGuard":"function isFreshBulkOp(bulk) {\n  return bulk != null && bulk.s != null && bulk.s.executed === false;\n}","tryCatchPattern":"try {\n  await bulk.execute();\n} catch (e) {\n  if (e instanceof MongoBatchReExecutionError) {\n    // create a fresh bulk op and rebuild operations\n  }\n}","preventionTips":["Never cache bulk op instances; create per use.","Build the operation list inside any retry loop, not outside it.","Prefer collection.bulkWrite(ops) for one-shot calls."],"tags":["bulk-write","lifecycle","retry","state-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}