{"record":{"id":"3da03eb7500056ec","repo":"mongodb/node-mongodb-native","slug":"timed-out-after-timeoutms-ms","errorCode":null,"errorMessage":"Timed out after ${timeoutMS}ms","messagePattern":"Timed out after (.+?)ms","errorType":"exception","errorClass":"MongoOperationTimeoutError","httpStatus":null,"severity":"error","filePath":"src/gridfs/index.ts","lineNumber":180,"sourceCode":"  async delete(id: ObjectId, options?: { timeoutMS: number }): Promise<void> {\n    const { timeoutMS } = resolveOptions(this.s.db, options);\n    let timeoutContext: CSOTTimeoutContext | undefined = undefined;\n\n    if (timeoutMS) {\n      timeoutContext = new CSOTTimeoutContext({\n        timeoutMS,\n        serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS\n      });\n    }\n\n    const { deletedCount } = await this.s._filesCollection.deleteOne(\n      { _id: id },\n      { timeoutMS: timeoutContext?.remainingTimeMS }\n    );\n\n    const remainingTimeMS = timeoutContext?.remainingTimeMS;\n    if (remainingTimeMS != null && remainingTimeMS <= 0)\n      throw new MongoOperationTimeoutError(`Timed out after ${timeoutMS}ms`);\n    // Delete orphaned chunks before returning FileNotFound\n    await this.s._chunksCollection.deleteMany({ files_id: id }, { timeoutMS: remainingTimeMS });\n\n    if (deletedCount === 0) {\n      // TODO(NODE-3483): Replace with more appropriate error\n      // Consider creating new error MongoGridFSFileNotFoundError\n      throw new MongoRuntimeError(`File not found for id ${id}`);\n    }\n  }\n\n  /** Convenience wrapper around find on the files collection */\n  find(filter: Filter<GridFSFile> = {}, options: FindOptions = {}): FindCursor<GridFSFile> {\n    return this.s._filesCollection.find(filter, options);\n  }\n\n  /**\n   * Returns a readable stream (GridFSBucketReadStream) for streaming the\n   * file with the given name from GridFS. If there are multiple files with","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/gridfs/index.ts#L162-L198","documentation":"GridFSBucket.delete (src/gridfs/index.ts:162) honors an optional timeoutMS. After deleting the file doc, it checks remainingTimeMS from the CSOT context; if the budget is exhausted it throws MongoOperationTimeoutError before deleting orphaned chunks. This enforces the client-side operation timeout for the delete operation.","triggerScenarios":"Calling bucket.delete(id, { timeoutMS: N }) where the deleteOne on the files collection plus overhead consumes the entire N. A slow or overloaded server causing the files-collection deleteOne to approach the budget.","commonSituations":"Setting an aggressive timeoutMS that is too small for the operation under load. Network latency or server-side locks consuming the budget before the chunks cleanup runs.","solutions":["Increase timeoutMS to a value that comfortably covers both the files and chunks deletions.","Remove timeoutMS for the call if a server-side default (socketTimeoutMS / serverSelectionTimeoutMS) is acceptable.","Investigate server load or indexing that makes the files-collection deleteOne slow."],"exampleFix":"// before\nawait bucket.delete(id, { timeoutMS: 100 });\n// after\nawait bucket.delete(id, { timeoutMS: 5000 });","handlingStrategy":"try-catch","validationCode":"// Estimate budget: delete must cover files + chunks collections.\n// Choose a timeout that comfortably exceeds worst-case latency.\nfunction generousTimeout(latencyMs) {\n  return Math.max(5000, latencyMs * 4);\n}","typeGuard":"function isTimeoutOption(v: unknown): v is { timeoutMS: number } {\n  return v != null && typeof (v as any).timeoutMS === 'number' && (v as any).timeoutMS > 0;\n}","tryCatchPattern":"try {\n  await bucket.delete(id, { timeoutMS });\n} catch (e) {\n  if (e instanceof MongoOperationTimeoutError && /Timed out/.test(e.message)) {\n    // retry with larger timeout or surface to caller\n  }\n  throw e;\n}","preventionTips":["Size timeoutMS to the slowest realistic server response, not the average.","Monitor delete latency to tune timeoutMS over time.","Prefer omitting timeoutMS unless you have a hard SLA."],"tags":["gridfs","timeout","csot"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}