Automattic/mongoose · error · MongooseError

First parameter to `deleteModel()` must be a string or regex

Error message

First parameter to `deleteModel()` must be a string or regexp, got "${name}"

What it means

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.

Source

Thrown at lib/connection.js:1576

    const model = this.model(name);
    if (model == null) {
      return this;
    }
    const collectionName = model.collection.name;
    delete this.models[name];
    delete this.collections[collectionName];

    this.emit('deleteModel', model);
  } else if (name instanceof RegExp) {
    const pattern = name;
    const names = this.modelNames();
    for (const name of names) {
      if (pattern.test(name)) {
        this.deleteModel(name);
      }
    }
  } else {
    throw new MongooseError('First parameter to `deleteModel()` must be a string ' +
      'or regexp, got "' + name + '"');
  }

  return this;
};

/**
 * Watches the entire underlying database for changes. Similar to
 * [`Model.watch()`](https://mongoosejs.com/docs/api/model.html#Model.watch()).
 *
 * This function does **not** trigger any middleware. In particular, it
 * does **not** trigger aggregate middleware.
 *
 * The ChangeStream object is an event emitter that emits the following events:
 *
 * - 'change': A change occurred, see below example
 * - '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.
 * - 'end': Emitted if the underlying stream is closed

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Pass the name: conn.deleteModel('X') or conn.deleteModel(Model.modelName)
  2. Delete several by pattern: conn.deleteModel(/^Temp/)
  3. Clear everything in teardown: conn.deleteModel(/./)

Example fix

// before
conn.deleteModel(mongoose.model('Test'));

// after
conn.deleteModel('Test'); // or mongoose.model('Test').modelName
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof name !== 'string' && !(name instanceof RegExp)) {
  throw new TypeError('deleteModel expects a model name string or RegExp');
}
conn.deleteModel(name);

Type guard

const isModelName = (v) => typeof v === 'string' || v instanceof RegExp;

Prevention

When it happens

Trigger: conn.deleteModel(mongoose.model('X')) (model instead of name); conn.deleteModel() with no argument; conn.deleteModel(['A', 'B']).

Common situations: Test cleanup between suites; hot-reload reset scripts; assuming deleteModel accepts the compiled class because other Mongoose APIs accept both names and classes.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/b2f3aff153898667. Report an issue: GitHub.