{"record":{"id":"0af57601a9b4cb62","repo":"microsoft/aspire","slug":"cannot-remove-string-join-removedhosts-select-h-h-from-the","errorCode":null,"errorMessage":"Cannot remove {string.Join(\", \", removedHosts.Select(h => $\"'{h}'\"))} from the existing MongoDB replica set '{rsResource.Name}': removing members from a replica set that has already been initialized is not supported yet. Add the member(s) back, or start over from an empty replica set by removing the data volumes of its members.","messagePattern":"Cannot remove (.+?)'\"\\)\\)\\} from the existing MongoDB replica set '(.+?)': removing members from a replica set that has already been initialized is not supported yet\\. Add the member\\(s\\) back, or start over from an empty replica set by removing the data volumes of its members\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs","lineNumber":247,"sourceCode":"                            using var existingClient = new MongoClient(existing.Connection.ConnectionString);\n                            var admin = existingClient.GetDatabase(\"admin\");\n                            var currentMembers = existing.Config[\"config\"][\"members\"].AsBsonArray;\n\n                            try\n                            {\n                                // NOTE: A forced reconfiguration that both drops a member and moves the remaining members' split\n                                // horizons — which is what restarting the app host does, since the host ports are reassigned —\n                                // leaves the surviving members unable to pick the new configuration up from each other, so the\n                                // replica set never elects a primary again. Rather than reconfiguring a persisted set into that\n                                // state, the removal is refused and the set is left on its current configuration.\n                                var removedHosts = currentMembers\n                                    .OfType<BsonDocument>()\n                                    .Select(m => m[\"host\"].AsString)\n                                    .Except(memberHosts.Select(m => m.Internal), StringComparer.OrdinalIgnoreCase)\n                                    .ToList();\n                                if (removedHosts.Count > 0)\n                                {\n                                    throw new DistributedApplicationException($\"Cannot remove {string.Join(\", \", removedHosts.Select(h => $\"'{h}'\"))} from the existing MongoDB replica set '{rsResource.Name}': removing members from a replica set that has already been initialized is not supported yet. Add the member(s) back, or start over from an empty replica set by removing the data volumes of its members.\");\n                                }\n\n                                var desiredMembers = BuildMembersConfiguration(memberHosts, currentMembers);\n\n                                // NOTE: A forced reconfiguration skips the checks that a normal one performs, so it is worth\n                                // not doing at all when it would change nothing. That is the common case for a set whose\n                                // members kept their addresses.\n                                if (MembersConfigurationMatches(currentMembers, desiredMembers))\n                                {\n                                    logger.LogInformation(\"MongoDB replica set resource '{ResourceName}' is already configured as declared — leaving it as it is\", resource.Name);\n                                    configured = true;\n                                    break;\n                                }\n\n                                logger.LogInformation(\"Re-configuring MongoDB replica set resource '{ResourceName}' from member '{MemberName}' — last version {Version}\", resource.Name, existing.Connection.Resource.Name, existing.Version);\n                                await admin.RunCommandAsync<BsonDocument>(\n                                    command: new BsonDocument\n                                    {","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs#L229-L265","documentation":"Once a MongoDB replica set has been initialized, the library only supports adding members, not removing them. If the desired member set is missing hosts that are present in the live replica set configuration, it throws DistributedApplicationException listing the removed hosts, because reconfiguring to drop members is not implemented.","triggerScenarios":"Calling AddMongoDBReplicaSet/WithMember with fewer members than a previously initialized replica set run - e.g. removing a WithMember call or a member resource from code after the replica set data volumes were already created and initialized.","commonSituations":"Refactoring app host code to drop one MongoDB node; temporarily commenting out a member; changing member count between local runs while persisting data volumes.","solutions":["Restore the removed member(s) in the app host code so the live configuration matches","Start over: stop the app host, delete the data volumes of all replica set members, then run with the new (smaller) member list","Keep the same member set across runs when data volumes persist"],"exampleFix":"// before (volume data persists from a 3-member run)\nvar rs = builder.AddMongoDBReplicaSet(\"rs\", mongo1, mongo2);\n// after (restore the third member or wipe volumes)\nvar rs = builder.AddMongoDBReplicaSet(\"rs\", mongo1, mongo2, mongo3);\n// or: docker volume rm <mongo volumes> and restart","handlingStrategy":"validation","validationCode":"// Compare intended members with the persisted set before changing code:\n// if data volumes exist from a prior run, keep the same member list or wipe the volumes first.","typeGuard":null,"tryCatchPattern":"try { /* run app host */ }\ncatch (DistributedApplicationException ex) when (ex.Message.Contains(\"removing members\")) { /* restore members or delete data volumes */ }","preventionTips":["Treat replica set member lists as append-only while data volumes persist","Wipe member data volumes when intentionally changing member count","Keep member definitions in a single clearly-marked place in the app host"],"tags":["mongodb","replica-set","unsupported-operation","stateful"],"backgroundTag":"unsupported-operation","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}