{"record":{"id":"6dea8f7b23fb217a","repo":"docker/cli","slug":"context-q-is-in-use-set-f-flag-to-force-remove","errorCode":null,"errorMessage":"context %q is in use, set -f flag to force remove","messagePattern":"context %q is in use, set -f flag to force remove","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cli/command/context/remove.go","lineNumber":54,"sourceCode":"func runRemove(dockerCLI command.Cli, opts removeOptions, names []string) error {\n\tvar errs []error\n\tcurrentCtx := dockerCLI.CurrentContext()\n\tfor _, name := range names {\n\t\tif name == \"default\" {\n\t\t\terrs = append(errs, errors.New(`context \"default\" cannot be removed`))\n\t\t} else if err := doRemove(dockerCLI, name, name == currentCtx, opts.force); err != nil {\n\t\t\terrs = append(errs, err)\n\t\t} else {\n\t\t\t_, _ = fmt.Fprintln(dockerCLI.Out(), name)\n\t\t}\n\t}\n\treturn errors.Join(errs...)\n}\n\nfunc doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {\n\tif isCurrent {\n\t\tif !force {\n\t\t\treturn fmt.Errorf(\"context %q is in use, set -f flag to force remove\", name)\n\t\t}\n\t\t// fallback to DOCKER_HOST\n\t\tcfg := dockerCli.ConfigFile()\n\t\tcfg.CurrentContext = \"\"\n\t\tif err := cfg.Save(); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tif !force {\n\t\t// TODO(thaJeztah): instead of checking before removing, can we make ContextStore().Remove() return a proper errdef and ignore \"not found\" errors?\n\t\tif err := checkContextExists(dockerCli, name); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn dockerCli.ContextStore().Remove(name)\n}\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/context/remove.go#L36-L72","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ndocker context rm prod\n# after (option A - force)\ndocker context rm -f prod\n# after (option B - switch first)\ndocker context use default && docker context rm prod","handlingStrategy":"validation","validationCode":"// Before removing, check whether the context is the current one.\nfunc safeRemove(cli command.Cli, name string, force bool) error {\n\tif name == cli.CurrentContext() && !force {\n\t\treturn fmt.Errorf(\"refusing to remove current context %q without force\", name)\n\t}\n\treturn cli.ContextStore().Remove(name)\n}","typeGuard":null,"tryCatchPattern":"if err := cli.ContextRemove(name); err != nil {\n\tif strings.Contains(err.Error(), \"is in use\") {\n\t\t// either switch context first or re-issue with -f\n\t}\n}","preventionTips":["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."],"tags":["docker-context","current-context","remove","safety-guard"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}