docker/cli · warning
context is in use, set -f flag to force remove
Error message
context %q is in use, set -f flag to force remove
What it means
Returned by doRemove (remove.go:54) when the user attempts to remove the context that is currently selected (CurrentContext) without passing --force. This is a safety guard: removing the active context would leave the CLI with no valid endpoint. The error message itself tells the user the exact remedy (set -f).
Solutions
- Re-run with -f if you genuinely want to remove the active context: 'docker context rm -f <name>'.
- Switch away first then remove without force: 'docker context use default && docker context rm <name>'.
- In scripts, capture 'docker context show' and skip or switch before removing.
- Unset DOCKER_CONTEXT if it is pinning the context you want to remove.
Example fix
# before docker context rm prod # after (option A - force) docker context rm -f prod # after (option B - switch first) docker context use default && docker context rm prod
Defensive patterns
Strategy: validation
Validate before calling
// Before removing, check whether the context is the current one.
func safeRemove(cli command.Cli, name string, force bool) error {
if name == cli.CurrentContext() && !force {
return fmt.Errorf("refusing to remove current context %q without force", name)
}
return cli.ContextStore().Remove(name)
} Try / catch
if err := cli.ContextRemove(name); err != nil {
if strings.Contains(err.Error(), "is in use") {
// either switch context first or re-issue with -f
}
} Prevention
- In scripts, run 'docker context use default' before tearing down named contexts.
- Pass -f only when you intentionally want to remove the active context.
- Check 'docker context show' before removing in automation.
When it happens
Trigger: Running 'docker context rm <current-context>' where <current-context> equals dockerCLI.CurrentContext() (set via 'docker context use', DOCKER_CONTEXT env var, or the config file's currentContext field) and the --force/-f flag is not supplied.
Common situations: Switching machines/daemons and forgetting the active context is the one being deleted; scripts that clean up contexts without checking which is active; CI that creates and tears down a context per run but forgot -f.
Related errors
- context does not exist
- cowardly refusing to save to a terminal. Use the -o flag or…
- container name cannot be empty
- failed to remove some resources from stack
- unable to get endpoint from context
AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07).
Data as JSON: /api/errors/6dea8f7b23fb217a.
Report an issue: GitHub.
Appendix: source
Thrown at cli/command/context/remove.go:54
func runRemove(dockerCLI command.Cli, opts removeOptions, names []string) error {
var errs []error
currentCtx := dockerCLI.CurrentContext()
for _, name := range names {
if name == "default" {
errs = append(errs, errors.New(`context "default" cannot be removed`))
} else if err := doRemove(dockerCLI, name, name == currentCtx, opts.force); err != nil {
errs = append(errs, err)
} else {
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
}
}
return errors.Join(errs...)
}
func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
if isCurrent {
if !force {
return fmt.Errorf("context %q is in use, set -f flag to force remove", name)
}
// fallback to DOCKER_HOST
cfg := dockerCli.ConfigFile()
cfg.CurrentContext = ""
if err := cfg.Save(); err != nil {
return err
}
}
if !force {
// TODO(thaJeztah): instead of checking before removing, can we make ContextStore().Remove() return a proper errdef and ignore "not found" errors?
if err := checkContextExists(dockerCli, name); err != nil {
return err
}
}
return dockerCli.ContextStore().Remove(name)
}
View on GitHub (pinned to 4f84911bfe)