{"record":{"id":"53b7956775dedaec","repo":"Automattic/mongoose","slug":"model-findbyidanddelete-no-longer-accepts-a-call","errorCode":null,"errorMessage":"Model.findByIdAndDelete() no longer accepts a callback","messagePattern":"Model\\.findByIdAndDelete\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":2601,"sourceCode":" *\n * This function triggers the following middleware.\n *\n * - `findOneAndDelete()`\n *\n * @param {object|number|string} id value of `_id` to query by\n * @param {object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())\n * @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)\n * @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.\n * @return {Query}\n * @see Model.findOneAndDelete https://mongoosejs.com/docs/api/model.html#Model.findOneAndDelete()\n * @see mongodb https://www.mongodb.com/docs/manual/reference/command/findAndModify/\n */\n\nModel.findByIdAndDelete = function(id, options) {\n  _checkContext(this, 'findByIdAndDelete');\n\n  if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {\n    throw new MongooseError('Model.findByIdAndDelete() no longer accepts a callback');\n  }\n\n  return this.findOneAndDelete({ _id: id }, options);\n};\n\n/**\n * Issue a MongoDB `findOneAndReplace()` command.\n *\n * Finds a matching document, replaces it with the provided doc, and returns the document.\n *\n * This function triggers the following query middleware.\n *\n * - `findOneAndReplace()`\n *\n * #### Example:\n *\n *     A.findOneAndReplace(filter, replacement, options)  // return Query\n *     A.findOneAndReplace(filter, replacement) // returns Query","sourceCodeStart":2583,"sourceCodeEnd":2619,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L2583-L2619","documentation":"Mongoose 7 removed callback support from all Model and Query APIs; findByIdAndDelete() delegates to findOneAndDelete({ _id: id }) and is promise-only. The guard throws this MongooseError synchronously if any of arguments[0] through arguments[2] is a function (id, options, or trailing callback).","triggerScenarios":"Calling Model.findByIdAndDelete(id, options, callback) or Model.findByIdAndDelete(id, callback) in Mongoose 7/8. Also fires when a function is mistakenly passed as the id.","commonSituations":"Mongoose 6 to 7/8 upgrades where delete-by-id call sites were not migrated; older codebases following pre-2022 tutorials that use callbacks for deletions.","solutions":["Use await: await Model.findByIdAndDelete(id); wrapped in try/catch","Or chain .then()/.catch() on the returned Query","Sweep the codebase for callback-style Model method calls as part of the Mongoose 7 migration checklist","Pin mongoose@6 as a stopgap only, with a migration plan","Enable TypeScript type-checking so callback overloads fail to compile"],"exampleFix":"// before\nUser.findByIdAndDelete(id, (err) => { ... });\n\n// after\nawait User.findByIdAndDelete(id);","handlingStrategy":"validation","validationCode":"function assertNoCallbacks(fnName, args) {\n  const i = args.findIndex(a => typeof a === 'function');\n  if (i !== -1) throw new TypeError(`${fnName}: callbacks removed in Mongoose 7; use await`);\n}\n// assertNoCallbacks('findByIdAndDelete', [id, options]);","typeGuard":null,"tryCatchPattern":"try {\n  await Model.findByIdAndDelete(id);\n} catch (err) {\n  if (/no longer accepts a callback/.test(err.message)) { /* migrate call site */ }\n  else throw err;\n}","preventionTips":["Grep for findByIdAndDelete(..., function / => patterns during upgrades","Keep delete helpers promise-based and centralize them so all call sites follow one style","TypeScript catches removed overloads at compile time"],"tags":["mongoose","callback","promise","migration","api-change","delete"],"backgroundTag":"callback-to-promise-migration","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}