Remove Shards from an Existing Sharded Cluster
On this page
To remove a shard you must ensure the shard’s data is migrated to the remaining shards in the cluster. This procedure describes how to safely migrate data and how to remove a shard.
When you remove a shard in a cluster with an uneven chunk distribution, the balancer first removes the chunks from the draining shard and then balances the remaining uneven chunk distribution.
This procedure describes how to safely remove a single shard. Do not use this procedure to migrate an entire cluster to new hardware. To migrate an entire shard to new hardware, migrate individual shards as if they were independent replica sets.
A shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.
To successfully migrate data from a shard, the balancer process must be enabled. Check the balancer state using the
sh.getBalancerState() helper in the
mongo shell. For more information, see the section on balancer operations.
listShardscommand, as in the following:
shards._id field lists the name of each shard.
admin database, run the
removeShard command. This begins “draining” chunks from the shard you are removing to other shards in the cluster. For example, for a shard named
This operation returns immediately, with the following response:
The balancer begins migrating chunks from the shard named
bristol01 to other shards in the cluster. These migrations happens slowly to avoid placing undue load on the overall cluster. Depending on your network capacity and the amount of data, this operation can take from a few minutes to several days to complete.
Each database in a sharded cluster has a primary shard. If the shard you want to remove is also the primary of one of the cluster’s databases,
removeShard lists the database in the
dbsToMove field. To finish removing the shard, you must either move the database to a new shard after migrating all data from the shard or drop the database, deleting the associated data files.
To check the progress of the migration at any stage in the process, run
removeShard from the
admin database again. For example, for a shard named
The command returns output similar to the following:
In the output, the
remaining document displays the remaining number of chunks that MongoDB must migrate to other shards and the number of MongoDB databases that have “primary” status on this shard.
Continue checking the status of the removeShard command until the number of chunks remaining is
0. Always run the command on the
admin database. If you are on a database other than
admin, you can use
sh._adminCommand to run the command on
If the shard is the primary shard for one or more databases in the cluster, then you must make that database use a different shard as its primary shard.
removeShard lists any databases that you need to move in the
dbsToMove field in the command output. If the shard is not the primary shard for any databases, skip to the next task, Finalize the Migration.
Do not perform this procedure until you have finished draining the shard.
To move a database to another shard, use the
To migrate the
fizz database from
mongodb1, issue the following command:
This command does not return until MongoDB completes moving all data. The response from this command will resemble the following:
If you use the
movePrimary command to move un-sharded collections, you must either restart all
mongos instances, or use the
flushRouterConfig command on all
mongos instances before reading or writing any data to any unsharded collections that were moved. This action ensures that the
mongos is aware of the new shard for these collections.
If you do not update the
mongos instances’ metadata cache after using
mongos may miss data on reads, and may not write data to the correct shard. To recover, you must manually intervene.
To clean up all metadata information and finalize the removal, run
removeShard again. For example, for a shard named
A success message appears at completion: