t8y2/dbx · error
Cannot delete the virtual host '%s' while connected to it
Error message
Cannot delete the virtual host '%s' while connected to it
What it means
Deleting a virtual host while the driver's AMQP connection is still attached to it leaves the connection invalid and can break cleanup. The driver proactively refuses to delete the vhost its own connection is using.
Source
Thrown at agents/drivers/rabbitmq/operations.go:374
}
func namespaceName(params jsonObject) (string, error) {
name := stringOrEmpty(params, "namespace")
if strings.TrimSpace(name) == "" {
return "", errors.New("namespace is required")
}
if strings.TrimSpace(name) == "*" {
return "", errors.New("namespace create/delete requires a specific virtual host (all-vhosts context)")
}
return name, nil
}
func assertNamespaceDeletable(namespace, connectedVhost string) error {
if namespace == "/" {
return errors.New("The default virtual host '/' cannot be deleted")
}
if connectedVhost != "" && namespace == connectedVhost {
return fmt.Errorf("Cannot delete the virtual host '%s' while connected to it", namespace)
}
return nil
}
func (s *server) listExchanges(params jsonObject) (any, error) {
connection, err := s.requireConnectionConfig(params)
if err != nil {
return nil, err
}
allVhosts := allVhostsRequested(params)
exchanges, err := managementGetAll(connection, managementListPath(params, connection, "exchanges"))
if err != nil {
return nil, err
}
result := make([]jsonObject, 0, len(exchanges))
for _, value := range exchanges {
exchange, ok := value.(map[string]any)
if !ok {View on GitHub (pinned to c0390bff16)
Solutions
- Connect (or reconfigure the connection) to a different vhost, e.g. the default '/', before deleting the target vhost
- Delete a different namespace name if the call used the wrong one
- Check the current connection's vhost and compare it to the delete target in your orchestration code
Example fix
// before
await client.deleteNamespace({"namespace": "myvhost"}) // while connected to myvhost
// after
await client.switchConnection({"vhost": "admin"})
await client.deleteNamespace({"namespace": "myvhost"}) Defensive patterns
Strategy: try-catch
Validate before calling
if (ns === currentConnection.vhost && ns !== "/") {
throw new Error(`switch connection off vhost '${ns}' before deleting it`)
} Try / catch
try {
await client.deleteNamespace({ namespace })
} catch (e) {
if (String(e.message).includes("while connected to it")) {
await client.switchConnection({ vhost: "admin" })
await client.deleteNamespace({ namespace })
} else { throw e }
} Prevention
- Track the active vhost of your connection and compare before deletes
- Use a dedicated admin connection (different vhost) for namespace lifecycle operations
- Filter the current vhost out of teardown lists
When it happens
Trigger: Calling deleteNamespace with a namespace equal to the vhost the current connection config points at (connectedVhost), e.g. deleting 'myvhost' while connected to 'myvhost'.
Common situations: Test teardown scripts that delete the namespace they are still using, re-running setup scripts after a connection was re-created against the same vhost, copy-pasting a delete call before switching connections.
Related errors
- The built-in exchange '%s' cannot be deleted
- Cannot %s user '%s' while connected as that user
- Not connected. Call connect first.
- namespace is required
- namespace create/delete requires a specific virtual host (al
AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05).
Data as JSON: /api/errors/4595fd926b805d1e.
Report an issue: GitHub.