Home / Alt manpages / docker-context-rm(1)

  • docker-context-rm(1)
  • User command
  • linux

Remove Stale Docker Contexts Without Deleting the Wrong Endpoint

You will finish with a safe way to identify and remove obsolete Docker CLI contexts, while avoiding the active context and keeping a recovery copy when the endpoint may still matter. Allow about ten minutes. You need the Docker CLI with context support and access to the Docker client configuration. The examples use Docker CLI 29.8.1, installed from docker-ce-cli on this machine.

A context is client-side connection information for a Docker daemon. Removing one deletes that client configuration; it does not remove containers, images or volumes from the daemon it pointed to. That distinction is useful, but the endpoint can still be a production or shared system, so check the destination before you remove its entry.

1. Confirm the command and version

Check the installed client before relying on examples. This is an ordinary, read-only command and does not need sudo:

$ docker version --format '{{.Client.Version}}'
29.8.1
$ docker context rm --help
Usage:  docker context rm CONTEXT [CONTEXT...]

Remove one or more contexts

Options:
  -f, --force   Force the removal of a context in use

The installed manual documents the same positional syntax and the single option -f or --force. The command accepts one or more context names. Do not assume that options from docker container rm also apply here.

Checkpoint

If your client is older or reports a different help page, stop and read that local output before copying the commands below.

2. List contexts and find the current one

List the names, descriptions and daemon endpoints:

$ docker context ls
NAME        DESCRIPTION                               DOCKER ENDPOINT   ERROR
default *   Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
staging                                                ssh://[email protected]
old-lab                                                unix:///var/run/docker.sock

The asterisk marks the active context. The endpoint is the part that deserves attention: a context name such as staging is only a label, while the endpoint tells you where later Docker commands would connect. The output above is illustrative; your names and endpoints will differ.

If the list is long, print only names and inspect candidates individually:

$ docker context ls --quiet
default
staging
old-lab
$ docker context inspect old-lab

docker context inspect shows the endpoint and other stored details. Treat an SSH target, a TCP target, or a context with TLS material as potentially sensitive and operationally significant. Do not remove a context merely because the daemon is currently unreachable. An unreachable endpoint may be temporary.

3. Save a context before deleting it

Export a context when you might need its connection details later. This is an ordinary client operation, but the resulting archive may contain credentials or TLS material, so store it with restrictive permissions:

$ umask 077
$ docker context export old-lab --output old-lab.dockercontext
Written file "old-lab.dockercontext"
$ chmod 600 old-lab.dockercontext
$ ls -l old-lab.dockercontext
-rw------- 1 you you ... old-lab.dockercontext

Docker's context documentation describes the export file as portable to another machine with the Docker client installed. Keep it in a protected location and do not upload it to a ticket or paste it into chat. If you know the context is disposable and its endpoint contains no useful credentials, this backup is optional, but it is cheap insurance for an uncertain cleanup.

Checkpoint

You should now have the exact candidate name, have checked its endpoint with inspect, and have an export if recovery is useful.

4. Remove an inactive context

Remove one or more named contexts by passing their exact names. Start with an inactive candidate:

$ docker context rm old-lab
old-lab
$ docker context ls --quiet
default
staging

A successful command prints the removed name. Removing the entry changes the Docker CLI configuration on this machine, but it does not contact the target daemon to delete workloads. It also does not switch any other machine that has a context with the same name.

For a batch cleanup, name each context explicitly after checking the list:

$ docker context rm old-lab test-daemon retired-build-host
old-lab
test-daemon
retired-build-host

Do not turn a generated list into a deletion command without reviewing it first. A shell expansion or an unexpected blank variable can make a destructive command harder to audit. Keep names quoted when they come from variables, and prefer a short, reviewed command.

5. Handle the active-context guard deliberately

Docker protects the context currently in use. If you try to remove it without force, the command should refuse and leave it in place:

$ docker context rm staging
Error: cannot remove context "staging", it is the current context
$ docker context ls
NAME        DESCRIPTION                               DOCKER ENDPOINT   ERROR
default     Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
staging *                                              ssh://[email protected]

The exact error wording can vary by client release, so rely on the non-zero exit status and the unchanged asterisk rather than matching the text character for character.

The safer response is to switch to a known-good context, then remove the old one:

$ docker context use default
default
Current context is now "default"
$ docker context show
default
$ docker context rm staging
staging

docker context use changes the default in your Docker CLI configuration. If you only need a one-off command against another context, use the global --context flag or the DOCKER_CONTEXT environment variable instead of changing the default. Check both the shell environment and the asterisk: DOCKER_CONTEXT can override the configured default.

6. Use force only when the active entry is definitely obsolete

--force exists for removing a context that is in use:

$ docker context rm --force staging
staging

This does not stop containers or delete data on the daemon. It removes the local context entry even though the CLI considers it current. It can, however, leave scripts, terminals or automation referring to a name that no longer exists. Prefer switching to default first. Use force only after checking the endpoint and confirming that no required workflow depends on the name.

There is no elevated-privilege requirement for removing a context from your own Docker configuration. sudo would use a different user's Docker configuration and could make you remove root's context instead of yours. Use elevated privileges only when the context files genuinely belong to another account and you have a separate administrative reason to manage that account's configuration.

7. Verify the cleanup and recover if needed

List the contexts again and confirm that the removed name is absent. Also check which context is now active:

$ docker context ls
$ docker context show
default

If the context was exported, recreate it with the same name from the protected archive:

$ docker context import old-lab old-lab.dockercontext
old-lab
Successfully imported context "old-lab"
$ docker context inspect old-lab

Inspect the imported endpoint before using it. Importing restores client configuration, not the daemon or any workloads. If you removed the only record of an endpoint and did not export it, Docker cannot reconstruct credentials or custom metadata from the removed name; recover from your normal configuration backup or recreate the context with verified values.

Done means

  • You confirmed the installed Docker CLI and its local context rm syntax.
  • You identified the exact context name and inspected its endpoint.
  • You exported uncertain or security-sensitive contexts before removal.
  • You removed only reviewed names, without using sudo unnecessarily.
  • You switched away from the active context, or consciously verified the need for --force.
  • docker context ls and docker context show confirm the intended final state.