Mongoose.prototype.deleteModel()

Parameters
  • name «String|RegExp» if string, the name of the model to remove. If regexp, removes all models whose name matches the regexp.
Returns:
  • «Mongoose» this

Removes the model named name from the default connection, if it exists. You can use this function to clean up any models you created in your tests to prevent OverwriteModelErrors.

Equivalent to mongoose.connection.deleteModel(name).

Example:

  1. mongoose.model('User', new Schema({ name: String }));
  2. console.log(mongoose.model('User')); // Model object
  3. mongoose.deleteModel('User');
  4. console.log(mongoose.model('User')); // undefined
  5. // Usually useful in a Mocha `afterEach()` hook
  6. afterEach(function() {
  7. mongoose.deleteModel(/.+/); // Delete every model
  8. });