docker/cli · error
context "default" cannot be removed
Error message
context "default" cannot be removed
What it means
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.
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"
Example fix
// before docker context rm default myctx // after docker context rm myctx
Defensive patterns
Strategy: validation
Validate before calling
for _, name := range names {
if name == "default" {
continue // never attempt to remove the default context
}
_ = dockerCLI.ContextStore().Remove(name)
} Prevention
- 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
When it happens
Trigger: Running `docker context rm default` (or passing "default" among a list of context names to remove).
Common situations: A cleanup script iterates `docker context ls` and tries to remove every entry including the synthetic default; bulk-reset automation.
Related errors
- conflicting options: cannot specify both --host and…
- cowardly refusing to export to a terminal, specify a file…
- no context specified
- unrecognized config key
- default context cannot be created nor updated
AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07).
Data as JSON: /api/errors/e30d870378ebda88.
Report an issue: GitHub.
Appendix: source
Thrown at cli/command/context/remove.go:41
Short: "Remove one or more contexts",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(dockerCLI, opts, args)
},
ValidArgsFunction: completeContextNames(dockerCLI, -1, false),
DisableFlagsInUseLine: true,
}
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force the removal of a context in use")
return cmd
}
// runRemove removes one or more contexts.
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 {View on GitHub (pinned to 4f84911bfe)