I’m working on a Discord bot and need help creating a command that wipes all user records from my MongoDB database. Right now I have code that only removes one user at a time, but I want to clear the entire collection.
This only deletes a single record but I need to remove all entries from the database. What’s the best way to modify this code to delete every document in the collection?
You can also use collection.drop() if you want to nuke the entire collection, not just the documents. This is way more aggressive than deleteMany since it wipes indexes too:
For Discord bots though, I’d stick with deleteMany like mentioned above. Pro tip: add safety checks before wiping data - maybe require confirmation from multiple admins or throw in a cooldown period. Trust me, I learned this the hard way after accidentally nuking production data during testing.
you could also use the remove() method - it’s deprecated but still works in older mongoose versions. I’d stick with deleteMany tho since it’s the current standard. also, use async/await instead of callbacks. makes debugging way easier when stuff breaks.
To wipe everything regardless of serverId, pass an empty object {} as the filter. I’ve done this in several Discord bots - works great. Just double-check your admin permissions since you can’t easily undo this.