{"record":{"id":"ca25e0a4f54e3dd3","repo":"Automattic/mongoose","slug":"connection-prototype-close-no-longer-accepts-a-c","errorCode":null,"errorMessage":"Connection.prototype.close() no longer accepts a callback","messagePattern":"Connection\\.prototype\\.close\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1215,"sourceCode":"    this.$wasForceClosed = !!force.force;\n  } else {\n    this.$wasForceClosed = !!force;\n  }\n\n  return this._close(force, true);\n};\n\n/**\n * Closes the connection\n *\n * @param {boolean} [force] optional\n * @return {Promise}\n * @api public\n */\n\nConnection.prototype.close = async function close(force) {\n  if (typeof force === 'function' || (arguments.length === 2 && typeof arguments[1] === 'function')) {\n    throw new MongooseError('Connection.prototype.close() no longer accepts a callback');\n  }\n\n  if (force != null && typeof force === 'object') {\n    this.$wasForceClosed = !!force.force;\n  } else {\n    this.$wasForceClosed = !!force;\n  }\n\n  if (this._lastHeartbeatAt != null) {\n    this._lastHeartbeatAt = null;\n  }\n\n  for (const model of Object.values(this.models)) {\n    // If manually disconnecting, make sure to clear each model's `$init`\n    // promise, so Mongoose knows to re-run `init()` in case the\n    // connection is re-opened. See gh-12047.\n    delete model.$init;\n  }","sourceCodeStart":1197,"sourceCodeEnd":1233,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1197-L1233","documentation":"close() gracefully closes the connection and is async in Mongoose 7+; all callback-style APIs were removed. Any function argument — as force, or as a second argument after force — trips the guard so legacy code fails loudly instead of the callback never running. An object as force ({ force: true }) remains supported; only functions throw.","triggerScenarios":"conn.close(() => { ... }) or conn.close(false, (err) => { ... }).","commonSituations":"afterAll/afterEach teardown in old test suites; shutdown hooks written against mongoose 6 or earlier.","solutions":["Remove the callback and await: await conn.close()","In test hooks: afterAll(async () => { await conn.close(); })"],"exampleFix":"// before\nconn.close((err) => { done(); });\n\n// after\nawait conn.close();","handlingStrategy":"validation","validationCode":"function closeSafe(...args) {\n  if (args.some(a => typeof a === 'function')) {\n    throw new TypeError('close takes no callback — await the promise');\n  }\n  return conn.close(...args);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write teardown as afterAll(async () => { await conn.close(); })","Pass { force: true } as an object when forced close is needed","Grep test helpers for close(done) patterns when upgrading mongoose majors"],"tags":["close","connection-lifecycle","callbacks","migration","mongoose-7"],"backgroundTag":"callback-api-removed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}