{"record":{"id":"e30d870378ebda88","repo":"docker/cli","slug":"context-default-cannot-be-removed","errorCode":null,"errorMessage":"context \"default\" cannot be removed","messagePattern":"context \"default\" cannot be removed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/context/remove.go","lineNumber":41,"sourceCode":"\t\tShort:   \"Remove one or more contexts\",\n\t\tArgs:    cli.RequiresMinArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn runRemove(dockerCLI, opts, args)\n\t\t},\n\t\tValidArgsFunction:     completeContextNames(dockerCLI, -1, false),\n\t\tDisableFlagsInUseLine: true,\n\t}\n\tcmd.Flags().BoolVarP(&opts.force, \"force\", \"f\", false, \"Force the removal of a context in use\")\n\treturn cmd\n}\n\n// runRemove removes one or more contexts.\nfunc 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 {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/context/remove.go#L23-L59","documentation":"runRemove() hard-blocks removal of the literal name \"default\". The default context is synthesized at runtime from DOCKER_HOST and config (never a stored entity), so deleting it is meaningless; the command refuses before touching the store.","triggerScenarios":"Running `docker context rm default` (or passing \"default\" among a list of context names to remove).","commonSituations":"A cleanup script iterates `docker context ls` and tries to remove every entry including the synthetic default; bulk-reset automation.","solutions":["Exclude \"default\" from the removal list","To change default behavior, set DOCKER_HOST / DOCKER_CONTEXT instead of deleting","Filter contexts before removing: skip any name equal to \"default\""],"exampleFix":"// before\ndocker context rm default myctx\n// after\ndocker context rm myctx","handlingStrategy":"validation","validationCode":"for _, name := range names {\n    if name == \"default\" {\n        continue // never attempt to remove the default context\n    }\n    _ = dockerCLI.ContextStore().Remove(name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Skip the literal name \"default\" in any context-removal loop","To reset default behavior, clear DOCKER_HOST/DOCKER_CONTEXT instead","Filter `docker context ls` output to exclude default before bulk rm"],"tags":["context","cli"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}