{"record":{"id":"b2f3aff153898667","repo":"Automattic/mongoose","slug":"first-parameter-to-deletemodel-must-be-a-strin","errorCode":null,"errorMessage":"First parameter to `deleteModel()` must be a string or regexp, got \"${name}\"","messagePattern":"First parameter to `deleteModel\\(\\)` must be a string or regexp, got \"(.+?)\"","errorType":"validation","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1576,"sourceCode":"    const model = this.model(name);\n    if (model == null) {\n      return this;\n    }\n    const collectionName = model.collection.name;\n    delete this.models[name];\n    delete this.collections[collectionName];\n\n    this.emit('deleteModel', model);\n  } else if (name instanceof RegExp) {\n    const pattern = name;\n    const names = this.modelNames();\n    for (const name of names) {\n      if (pattern.test(name)) {\n        this.deleteModel(name);\n      }\n    }\n  } else {\n    throw new MongooseError('First parameter to `deleteModel()` must be a string ' +\n      'or regexp, got \"' + name + '\"');\n  }\n\n  return this;\n};\n\n/**\n * Watches the entire underlying database for changes. Similar to\n * [`Model.watch()`](https://mongoosejs.com/docs/api/model.html#Model.watch()).\n *\n * This function does **not** trigger any middleware. In particular, it\n * does **not** trigger aggregate middleware.\n *\n * The ChangeStream object is an event emitter that emits the following events:\n *\n * - 'change': A change occurred, see below example\n * - 'error': An unrecoverable error occurred. In particular, change streams currently error out if they lose connection to the replica set primary. Follow [this GitHub issue](https://github.com/Automattic/mongoose/issues/6799) for updates.\n * - 'end': Emitted if the underlying stream is closed","sourceCodeStart":1558,"sourceCodeEnd":1594,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1558-L1594","documentation":"deleteModel() removes compiled models from the connection and accepts exactly a string (one model name) or a RegExp (delete all matching names). Any other type — undefined, a compiled Model class, an array, a number — throws (lib/connection.js:1576). Passing a Model class instead of its name is the most common trip.","triggerScenarios":"conn.deleteModel(mongoose.model('X')) (model instead of name); conn.deleteModel() with no argument; conn.deleteModel(['A', 'B']).","commonSituations":"Test cleanup between suites; hot-reload reset scripts; assuming deleteModel accepts the compiled class because other Mongoose APIs accept both names and classes.","solutions":["Pass the name: conn.deleteModel('X') or conn.deleteModel(Model.modelName)","Delete several by pattern: conn.deleteModel(/^Temp/)","Clear everything in teardown: conn.deleteModel(/./)"],"exampleFix":"// before\nconn.deleteModel(mongoose.model('Test'));\n\n// after\nconn.deleteModel('Test'); // or mongoose.model('Test').modelName","handlingStrategy":"type-guard","validationCode":"if (typeof name !== 'string' && !(name instanceof RegExp)) {\n  throw new TypeError('deleteModel expects a model name string or RegExp');\n}\nconn.deleteModel(name);","typeGuard":"const isModelName = (v) => typeof v === 'string' || v instanceof RegExp;","tryCatchPattern":null,"preventionTips":["Pass Model.modelName, not the Model class","Use a RegExp to clear many models at once: conn.deleteModel(/./) in test teardown","Remember deleteModel returns the connection for chaining after a valid name"],"tags":["model","deletemodel","invalid-argument-type","api-misuse"],"backgroundTag":"invalid-argument-type","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}