{"record":{"id":"676ceef7d0ec2263","repo":"Automattic/mongoose","slug":"query-prototype-findoneanddelete-no-longer-accep","errorCode":null,"errorMessage":"Query.prototype.findOneAndDelete() no longer accepts a callback","messagePattern":"Query\\.prototype\\.findOneAndDelete\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/query.js","lineNumber":3685,"sourceCode":" *\n * @method findOneAndDelete\n * @memberOf Query\n * @param {object} [filter]\n * @param {object} [options]\n * @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document\n * @param {boolean} [options.requireFilter=false] If true, throws an error if the filter is empty (`{}`)\n * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).\n * @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)\n * @return {Query} this\n * @see findAndModify command https://www.mongodb.com/docs/manual/reference/command/findAndModify/\n * @api public\n */\n\nQuery.prototype.findOneAndDelete = function(filter, options) {\n  if (typeof filter === 'function' ||\n      typeof options === 'function' ||\n      typeof arguments[2] === 'function') {\n    throw new MongooseError('Query.prototype.findOneAndDelete() no longer accepts a callback');\n  }\n\n  this.op = 'findOneAndDelete';\n  this._validate();\n\n  if (canMerge(filter)) {\n    this.merge(filter);\n  }\n\n  options && this.setOptions(options);\n\n  return this;\n};\n\n/**\n * Execute a `findOneAndDelete()` query\n *\n * @return {Query} this","sourceCodeStart":3667,"sourceCodeEnd":3703,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/query.js#L3667-L3703","documentation":"Query.prototype.findOneAndDelete(filter, options) throws when filter, options, or a third positional argument is a function — the Mongoose 7 promise-only rule. The throw happens while building the query, before the delete command is sent, so legacy callback invocations fail fast.","triggerScenarios":"Model.findOneAndDelete({ _id }, (err, doc) => {...}); .findOneAndDelete(cb) with the callback in the filter slot; a function passed in the options slot.","commonSituations":"Session/token cleanup code and account-deletion routes written for Mongoose 6; upgrades to mongoose 7/8; old middleware chains that forward callbacks.","solutions":["Use await: const doc = await Model.findOneAndDelete({ _id }); doc is null when nothing matched","Wrap in try/catch for error handling","Remove trailing callbacks from all findOneAndDelete call sites during upgrade"],"exampleFix":"// before\nModel.findOneAndDelete({ _id }, (err, doc) => {\n  if (err) return next(err);\n  res.json(doc);\n});\n\n// after\ntry {\n  const doc = await Model.findOneAndDelete({ _id });\n  res.json(doc);\n} catch (err) {\n  next(err);\n}","handlingStrategy":"validation","validationCode":"function findOneAndDeleteSafe(model, filter, options) {\n  if ([filter, options].some(v => typeof v === 'function')) {\n    throw new Error('findOneAndDelete() is promise-only in Mongoose 7+');\n  }\n  return model.findOneAndDelete(filter, options);\n}","typeGuard":"const isLegacyCallback = (v) => typeof v === 'function';","tryCatchPattern":"try {\n  const doc = await Model.findOneAndDelete({ _id });\n} catch (err) {\n  if (err?.message?.includes('no longer accepts a callback')) {\n    // fix the findOneAndDelete call site that still passes a callback\n  }\n  throw err;\n}","preventionTips":["Await findOneAndDelete in token/session cleanup code","Handle the null result for no match","Upgrade session and auth middleware together with mongoose to avoid mixed styles"],"tags":["mongoose","callback","promise","migration","breaking-change","delete"],"backgroundTag":"legacy-callback-removed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}