Removing nodes from MongoDB Replica Set
-
Danilo Pala
- 23 Dec, 2025
- 02 Mins read
Removing nodes from MongoDB Replica Set
In this article I'm going to talk about removing nodes from MongoDB Replica Sets managed by OPS Manager.
As common as adding nodes to a Replica Set (RS), removing nodes from a RS is a pretty frequent task for a MongoDB DBA, if the RS is managed manually, we have to use the command rs.remove(), but if the RS is managed by OPS Manager we can use the UI or the API.
In order to automate these tasks by using OPS Manager API, I've developed a python script (version 3.9) that allow us to remove a node from the Replica Set via command line.
The script accept the following arguments:
--project_id: project id
--cluster_name: replica set name
--remove_node: node with format <hostname.fqdn:port>
There are more arguments, but I'm going to omit in this article.
An API key is required for invoking OPS Manager API, and it must have permission to edit deployments of the project.
Behavior
First, the script validates the inputted data, then invokes the OPS Manager API getting the automationConfig of the project_id, the response is a json that's loaded into a dictionary.
In the "processes" field, the script identifies the node passed as remove_node, and delete it from the array.
In the "replicaSets" field, the procedure is similar to the "process" field, where the script identifies the RS matching the cluster_name argument with the Replica Set Name, and it will identify and delete the member that matches with the remove_node.
Then the script requests a put to the OPS Manager API passing the modified automationConfig, finally it will wait until all processes achieve the goal version to return the final result.
Example
I have Replica Set with the following configuration:

Then I launched the script rs_remove.py (I masked sensible data):
python3.9 rs_remove.py --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME --remove_node="**.local:27031"
on terminal

As result, at the OPS Manager UI:

Conclusion
This the simpliest script in comparison with the others, but it's powerfull and able to cover any removing use cases, we just need to pass the node as argument and it will be removed from the Replica Set.
In the next article I'going to put all 3 scripts together in a migration, stay tuned.