How to delete system.profile collection from a MongoDB?
MongodbProfilingMongodb Problem Overview
I am profiling the db queries in MongoDB. I followed this link. I am trying to delete all data from the collection system.profile so I can start benchmarking different queries again. I tried the following code but it gives an error
CONSOLE SYNTAX
> db.system.profile.remove({})
ERROR
cannot delete from system namespace
How do I delete all data from that collection? If that's not possible, how can I start profiling from the beginning?
Mongodb Solutions
Solution 1 - Mongodb
Firstly, turn off the profiling by setting its level to 0.
db.setProfilingLevel(0)
Then you can simply drop the collection.
db.system.profile.drop()
Now you are free to go and start it all over.
Solution 2 - Mongodb
To riff off of the accepted answer, it is at times helpful to do in a single line:
db.setProfilingLevel(0); db.system.profile.drop(); db.setProfilingLevel(2);
I'm also finding that setting the size of system.profile to 10MB (or larger) rather than its default 1MB is quite helpful:
db.setProfilingLevel(0); db.system.profile.drop(); db.createCollection("system.profile", {capped: true, size: 10 * 1024 * 1024}); db.setProfilingLevel(2);
Solution 3 - Mongodb
Keep in mind that the profile collection is capped!
In order to create a collection keeping more records about queries, recreate it with a desired size.
db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size: 1024*1024*10 } )
db.setProfilingLevel(1, { slowms: 500 })