- Replication >
- Replica Set Tutorials >
- Replica Set Deployment Tutorials >
- Remove Members from Replica Set
Remove Members from Replica Set¶
To remove a member of a replica set use either of the following procedures.
Remove a Member Using rs.remove()¶
Shut down the
mongodinstance for the member you wish to remove. To shut down the instance, connect using themongoshell and thedb.shutdownServer()method.Connect to the replica set’s current primary. To determine the current primary, use
db.isMaster()while connected to any member of the replica set.Use
rs.remove()in either of the following forms to remove the member:MongoDB disconnects the shell briefly as the replica set elects a new primary. The shell then automatically reconnects. The shell displays a
DBClientCursor::init call() failederror even though the command succeeds.
Remove a Member Using rs.reconfig()¶
To remove a member you can manually edit the replica set configuration document, as described here.
Shut down the
mongodinstance for the member you wish to remove. To shut down the instance, connect using themongoshell and thedb.shutdownServer()method.Connect to the replica set’s current primary. To determine the current primary, use
db.isMaster()while connected to any member of the replica set.Issue the
rs.conf()method to view the current configuration document and determine the position in themembersarray of the member to remove:Example
mongod_C.example.netis in position2of the following configuration file:Assign the current configuration document to the variable
cfg:Modify the
cfgobject to remove the member.Example
To remove
mongod_C.example.net:27017use the following JavaScript operation:Overwrite the replica set configuration document with the new configuration by issuing the following:
As a result of
rs.reconfig()the shell will disconnect while the replica set renegotiates which member is primary. The shell displays aDBClientCursor::init call() failederror even though the command succeeds, and will automatically reconnected.To confirm the new configuration, issue
rs.conf().For the example above the output would be: