Remove Members from Replica Set

To remove a member of a replica set use either of thefollowing procedures.

Remove a Member Using rs.remove()

  • Shut down the mongod instance for the member you wish toremove. To shut down the instance, connect using themongo shell and the db.shutdownServer()method.

  • Connect to the replica set’s current primary. To determinethe current primary, use db.isMaster() while connected toany member of the replica set.

  • Use rs.remove() in either of the following forms toremove the member:

  1. rs.remove("mongod3.example.net:27017")
  2. rs.remove("mongod3.example.net")

MongoDB may disconnect the shell briefly if the replica set needs to elect anew primary. The shell then automatically reconnects in such cases. Theshell may display a DBClientCursor::init call() failed error eventhough the command succeeds.

Remove a Member Using rs.reconfig()

To remove a member you can manually edit the replica setconfiguration document, as describedhere.

  • Shut down the mongod instance for the member you wish toremove. To shut down the instance, connect using themongo shell and the db.shutdownServer()method.

  • Connect to the replica set’s current primary. To determinethe current primary, use db.isMaster() while connected toany member of the replica set.

  • Issue the rs.conf() method to view the currentconfiguration document and determine the position in themembers array of the member to remove:

Example

mongod_C.example.net is in position 2 of thefollowing configuration file:

  1. {
  2. "_id" : "rs",
  3. "version" : 7,
  4. "members" : [
  5. {
  6. "_id" : 0,
  7. "host" : "mongod_A.example.net:27017"
  8. },
  9. {
  10. "_id" : 1,
  11. "host" : "mongod_B.example.net:27017"
  12. },
  13. {
  14. "_id" : 2,
  15. "host" : "mongod_C.example.net:27017"
  16. }
  17. ]
  18. }
  • Assign the current configuration document to the variable cfg:
  1. cfg = rs.conf()
  • Modify the cfg object to remove the member.

Example

To remove mongod_C.example.net:27017 use the followingJavaScript operation:

  1. cfg.members.splice(2,1)
  • Overwrite the replica set configuration document with the newconfiguration by issuing the following:
  1. rs.reconfig(cfg)
  • To confirm the new configuration, issue rs.conf().

For the example above the output would be:

  1. {
  2. "_id" : "rs",
  3. "version" : 8,
  4. "members" : [
  5. {
  6. "_id" : 0,
  7. "host" : "mongod_A.example.net:27017"
  8. },
  9. {
  10. "_id" : 1,
  11. "host" : "mongod_B.example.net:27017"
  12. }
  13. ]
  14. }