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

  1. Exclude "default" from the removal list
  2. To change default behavior, set DOCKER_HOST / DOCKER_CONTEXT instead of deleting
  3. 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

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


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)