Clear Logging From Mongo DB

In some situations, LogMessages are not automatically deleted from the collection, causing performance issues. To resolve this issue, a cleanup script was introduced.

Mongo Command- db.collection.remove() :

The db.collection.remove() method is used to delete collection data from collection.

The syntax for this command is as follows: db.collection.remove()


A batch file is provided which when executes deletes logmessageentity collection data from Mongo DB whose name is provided in the batch file connection string:-

cd \Program Files\MongoDB\Server\3.2\bin set /p Count= Enter the no. of days since when the logging should be removed: mongo.exe AC-SVR024:27017/Znode_9_0_0_Custom_LogMessage --eval "db.logmessageentity.remove({ CreatedDate:{ $lte : new Date(ISODate().getTime() - 1000 * 86400 * "%Count%")} });" pause

Steps for executing batch file :

  1. Edit the batch file and modify the connection string.

  2. Modify server name and Mongo DB name.

  3. Provide the number of days from which the log should be cleared.

  4. Save the batch file and execute it.

  5. The logmessageentity collection data generated X days ago will be cleared. Here X represents the number of days provided.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.