{"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":"error","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/e9452d6e785f6e365712b9d71bd7517591773c86/cli/command/context/remove.go#L36-L72","documentation":"Error \"context %q is in use, set -f flag to force remove\" thrown in docker/cli.","triggerScenarios":"Thrown at cli/command/context/remove.go:54 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"e9452d6e785f6e365712b9d71bd7517591773c86","analyzedAt":"2026-08-01T07:09:22.474Z","schemaVersion":2}