{"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/e9452d6e785f6e365712b9d71bd7517591773c86/cli/command/context/remove.go#L23-L59","documentation":"The 'default' context is synthesized at runtime from DOCKER_HOST, DOCKER_TLS settings, and CLI flags — it is not a file in the context store. `docker context rm` therefore special-cases the name 'default' in runRemove and refuses to delete it; removal continues for any other names given and the failures are joined into one error.","triggerScenarios":"`docker context rm default` or any `docker context rm` invocation whose argument list includes the literal name 'default' (cli/command/context/remove.go:40-41). --force does not bypass this check — force only applies to removing the context currently in use.","commonSituations":"Cleanup scripts doing `docker context rm $(docker context ls -q)` which sweeps in 'default'; users trying to 'reset' Docker Desktop's context setup; attempts to remove the default entry to stop it appearing in `docker context ls`.","solutions":["Exclude 'default' from bulk removals: `docker context ls -q | grep -v '^default$' | xargs -r docker context rm`.","To change what the default points at, set DOCKER_HOST or use `docker context use <other>` instead of removing it.","Remove only the named custom contexts you created; 'default' is virtual and costs nothing."],"exampleFix":"# before\ndocker context rm $(docker context ls -q)\n# after\ndocker context ls -q | grep -v '^default$' | xargs -r docker context rm","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["docker-cli","context","default-context"],"analyzedSha":"e9452d6e785f6e365712b9d71bd7517591773c86","analyzedAt":"2026-08-01T07:09:22.474Z","schemaVersion":2}