How to Fix MongoDB Error 370 (CannotDropShardKeyIndex)
You trigger mongodb error 370 when you try to drop the last index supporting a shard key. To fix it, build an alternate index for the shard key before dropping the old one. In MongoDB, this error has the official name CannotDropShardKeyIndex. The database blocks the drop request to keep your sharded cluster working. A sharded collection always needs at least one valid index to route reads and writes. Without that index, the cluster cannot find where data lives across your shards. Root causes
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You trigger mongodb error 370 when you try to drop the last index supporting a shard key. To fix it, build an alternate index for the shard key before dropping the old one.
In MongoDB, this error has the official name CannotDropShardKeyIndex. The database blocks the drop request to keep your sharded cluster working. A sharded collection always needs at least one valid index to route reads and writes. Without that index, the cluster cannot find where data lives across your shards.
Root causes of mongodb error 370 in sharded clusters
Sharded collections use a shard key to split documents across multiple servers. The database relies on an index that starts with the exact shard key fields. When a user or script tries to remove that index, the engine stops the action. It returns code 370 to prevent broken routing and cluster instability.
This failure often happens during routine index cleanup or schema migrations. A team member might look for duplicate or large indexes to save disk space. They see a compound index and decide to remove it. If that index is the only one covering the shard key prefix, the command fails.
Automated migration tools can also cause this problem during deployment runs. Some tools drop an old index before creating the updated version. On a normal collection, that drop and recreate sequence works fine. On a sharded collection, the database rejects the drop step right away.
How to diagnose and fix the index failure safely
First, check the current indexes on your target collection and review the shard key. You only need a basic read role to run index checks safely. The built-in read role lets you list collections and list indexes without risk. It does not allow data edits or index drops.
Look at all existing indexes on the collection. Find which index starts with the fields defined in your shard key. If only one index matches that prefix, you cannot drop it directly. You must give the cluster a new index to use first.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
To replace the index, create the new index before removing the old one. The new index must start with the exact same shard key fields. It can include extra fields at the end if your queries need a compound index. Wait for the new index build to finish across all shards in the cluster.
Once the new index is ready, the cluster has two valid shard key indexes. You can now drop the old index without triggering code 370. If you actually want to drop the shard key itself, you must refine the key first. Otherwise, you would have to move the data to an unsharded collection.
Preventing accidental index drops with role controls
Production outages often happen when too many accounts can drop indexes. In MongoDB, the standard readWrite role includes more than just insert, update, and remove actions. It also grants permissions to create collections, create indexes, and drop indexes. Giving every developer or app the readWrite role exposes your cluster to accidental schema changes.
You can prevent mistakes by matching roles to daily job needs. MongoDB grants access through roles that apply down to the collection level. Give engineers the read role for everyday checks and query tuning. When someone needs to drop an index, require a peer review for that specific command.
You should also mask sensitive fields during index and data checks. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This lets engineers inspect documents and shard keys safely during production fixes.
Check MongoDB commands and documents on the wire
MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.
Frequently asked questions
What does CannotDropShardKeyIndex mean in MongoDB?
CannotDropShardKeyIndex is the official name for error code 370 in MongoDB. It means you tried to delete the only index that supports a collection shard key.
How can I replace a compound index that covers my shard key?
Create the new index first and make sure it starts with the shard key fields. After the new index finishes building on all shards, drop the old index.
Which built-in MongoDB role allows a user to drop an index?
The built-in readWrite role includes the action to drop indexes on its database. The read role only allows listing indexes and reading data, which prevents accidental drops.
Related guides
In this post