microsoft/aspire · error · DistributedApplicationException
Cannot remove '"))} from the existing MongoDB replica set '…
Error message
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. What it means
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.
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
Example fix
// before (volume data persists from a 3-member run)
var rs = builder.AddMongoDBReplicaSet("rs", mongo1, mongo2);
// after (restore the third member or wipe volumes)
var rs = builder.AddMongoDBReplicaSet("rs", mongo1, mongo2, mongo3);
// or: docker volume rm <mongo volumes> and restart Defensive patterns
Strategy: validation
Validate before calling
// Compare intended members with the persisted set before changing code: // if data volumes exist from a prior run, keep the same member list or wipe the volumes first.
Try / catch
try { /* run app host */ }
catch (DistributedApplicationException ex) when (ex.Message.Contains("removing members")) { /* restore members or delete data volumes */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Refactoring app host code to drop one MongoDB node; temporarily commenting out a member; changing member count between local runs while persisting data volumes.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Cannot build connection string for MongoDB replica set…
- Connection string is unavailable
- Failed to configure MongoDB replica set resource
- MongoDB replica set member
- The connection string of MongoDB replica set member
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/0af57601a9b4cb62.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs:247
using var existingClient = new MongoClient(existing.Connection.ConnectionString);
var admin = existingClient.GetDatabase("admin");
var currentMembers = existing.Config["config"]["members"].AsBsonArray;
try
{
// NOTE: A forced reconfiguration that both drops a member and moves the remaining members' split
// horizons — which is what restarting the app host does, since the host ports are reassigned —
// leaves the surviving members unable to pick the new configuration up from each other, so the
// replica set never elects a primary again. Rather than reconfiguring a persisted set into that
// state, the removal is refused and the set is left on its current configuration.
var removedHosts = currentMembers
.OfType<BsonDocument>()
.Select(m => m["host"].AsString)
.Except(memberHosts.Select(m => m.Internal), StringComparer.OrdinalIgnoreCase)
.ToList();
if (removedHosts.Count > 0)
{
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.");
}
var desiredMembers = BuildMembersConfiguration(memberHosts, currentMembers);
// NOTE: A forced reconfiguration skips the checks that a normal one performs, so it is worth
// not doing at all when it would change nothing. That is the common case for a set whose
// members kept their addresses.
if (MembersConfigurationMatches(currentMembers, desiredMembers))
{
logger.LogInformation("MongoDB replica set resource '{ResourceName}' is already configured as declared — leaving it as it is", resource.Name);
configured = true;
break;
}
logger.LogInformation("Re-configuring MongoDB replica set resource '{ResourceName}' from member '{MemberName}' — last version {Version}", resource.Name, existing.Connection.Resource.Name, existing.Version);
await admin.RunCommandAsync<BsonDocument>(
command: new BsonDocument
{View on GitHub (pinned to 25830f84bd)