REW

How Do I Remove A Node From A Replica Set In MongoDB?

Published Aug 29, 2025 5 min read
On this page

Here is a comprehensive guide on how to remove a node from a MongoDB replica set. You can choose from two primary methods: using the rs.remove() helper function or directly reconfiguring the replica set with rs.reconfig(). Both methods require you to connect to the current primary node of the replica set.

Before you begin

Before you remove a replica set member, it's essential to follow these preparatory steps to ensure data integrity and a smooth transition:

  • Identify the current primary: Connect to any replica set member and run the rs.status() or db.hello() command to determine the current primary. This is the node you will connect to for the removal operation.

  • Back up your data: Always create a backup of your replica set data before performing any critical maintenance operation.

  • Ensure replica set health: Verify that the replica set is healthy and all members are communicating correctly by checking the output of rs.status().

  • Shut down the target node: Gracefully shut down the mongod instance of the member you plan to remove. This prevents any inconsistencies and ensures no data is written to the node after its removal from the replica set configuration.javascript

    use admin
    db.shutdownServer()
    

    Use code with caution.

Method 1: Use rs.remove()

The rs.remove() command is the simplest way to remove a single member from a replica set. It automatically updates the replica set configuration.

Step-by-step procedure

  1. Connect to the primary: Using mongosh, connect to the current primary node of your replica set.

  2. Execute the rs.remove() command: Run rs.remove() with the host and port of the member you want to remove.javascript

    rs.remove("hostname_or_ip:port")
    

    Use code with caution.

    • Example:rs.remove("mongod3.example.net:27017")
    • Behavior: MongoDB will automatically perform a reconfiguration of the replica set. The primary may step down, and a new election will occur to choose a new primary. If this happens, your shell may briefly disconnect before automatically reconnecting.
  3. Verify the new configuration: After the command completes, run rs.status() or rs.conf() to confirm that the member has been successfully removed from the replica set.javascript

    rs.status()
    

    Use code with caution.

  4. Clean up the removed node: The removed mongod instance still has its old data and configuration. You should perform the following cleanup tasks:

    • Delete the data directory of the removed member to prevent it from rejoining the set with old data.
    • If you plan to repurpose the node as a standalone instance, update its configuration file to remove the replication.replSetName setting.

Method 2: Use rs.reconfig()

The rs.reconfig() method offers greater control over the reconfiguration process by allowing you to manually edit the replica set configuration document. This is especially useful for removing multiple members or performing more complex configuration changes.

Step-by-step procedure

  1. Connect to the primary: Connect to the current primary node using mongosh.

  2. Get the current configuration: Retrieve the current replica set configuration document by running rs.conf() and assign it to a variable.javascript

    cfg = rs.conf()
    

    Use code with caution.

  3. Find the member to remove: Identify the member you want to remove within the members array of the configuration document. The _id field is the index of the member in the array.json

    {
      "_id" : "rs",
      "version" : 7,
      "members" : [
        { "_id" : 0, "host" : "mongod_A.example.net:27017" },
        { "_id" : 1, "host" : "mongod_B.example.net:27017" },
        { "_id" : 2, "host" : "mongod_C.example.net:27017" }
      ]
    }
    

    Use code with caution.

  4. Modify the configuration: Use the Array.prototype.splice() method in JavaScript to remove the member from the members array. For example, to remove the member with _id: 2, you would use:javascript

    cfg.members.splice(2, 1)
    

    Use code with caution.

  5. Apply the new configuration: Pass the modified configuration document to the rs.reconfig() method.javascript

    rs.reconfig(cfg)
    

    Use code with caution.

    • Note: If you are removing multiple voting members, you must perform this operation one member at a time to maintain a stable majority.
  6. Verify and clean up: As with the rs.remove() method, verify the new configuration with rs.status() and then clean up the removed mongod instance's data directory.

Considerations for removing the primary

Removing a primary node is a more complex operation because it necessitates a failover and election process.

  • Planned failover: Before removing the current primary, you can force a failover to a different member. This gives you greater control over which node becomes the new primary. You can do this by using the rs.stepDown() command on the current primary.
  • Election: When you remove the current primary, an election will immediately be triggered among the remaining members to choose a new primary. This might cause a brief outage until the election completes.

What happens after a node is removed?

After a node is removed from the replica set, the following occurs:

  • State change: The removed member, if still running, will enter a "REMOVED" state. The remaining replica set members will no longer attempt to communicate with it.
  • Configuration update: The replica set's configuration document is updated across all members to reflect the new state.
  • Cleanup: The data directory on the removed machine remains intact. It is crucial to manually delete this data or reconfigure the node for standalone use to prevent a desynchronized member from accidentally rejoining with a "divergent write history," which can cause data inconsistencies.
Enjoyed this article? Share it with a friend.