How to reuse dropped sharded collection’s name
It happens that sometimes you want to drop your sharded collection and be able to reuse its name again. However, it might not be as straightforward as one expects it to be:
mongos>sh.shardColelction("your_database.your_collection", { "sharded_key": 1}) "code" : 13449, "ok" : 0, "errmsg" : "exception: collection your_database.your_collection already sharded"
The error message might be different but you get the idea – you can’t shared a collection if its name matches the one that has been recently dropped. Thankfully, there is a workaround described in SERVER-17397:
When dropping a collection: use config db.collections.remove( { _id: "DATABASE.COLLECTION" } ) db.chunks.remove( { ns: "DATABASE.COLLECTION" } ) db.locks.remove( { _id: "DATABASE.COLLECTION" } ) Connect to each mongos and run flushRouterConfig
Followed the steps in prod yesterday and it worked like a charm.