Node.js: How to release Mongoose model from memory? (memory leak) Node.js: How to release Mongoose model from memory? (memory leak) mongoose mongoose

Node.js: How to release Mongoose model from memory? (memory leak)


Mongoose exposes the model and schema caches via a property of either mongoose or the connection (depending on which you used to declare the model):

[mongoose or connection ].models // an object

[mongoose or connection ].modelSchemas // an object

Each is an object that uses the declared model name as a property name. You can then delete each model if you want to manually garbage collect them.


These lines did the trick and got all my memory back:

delete db.models[collection];delete db.collections[collection];delete db.base.modelSchemas[collection];