Home / Alt manpages / docker-rm(1)

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

Remove Docker Containers Without Losing the Wrong Data

You will finish with a repeatable way to remove one or more Docker containers, stop a running container cleanly first, and decide what happens to its anonymous volumes. The examples match Docker CE CLI 29.8.1, installed here as package version 5:29.8.1-1~ubuntu.24.04~noble.

Allow about ten minutes for a single container. You need a shell and access to the Docker daemon. Most commands are ordinary user commands. Use sudo only if your account cannot access the Docker socket and your local Docker setup expects administrative access. The Docker daemon may be running as root, but that does not make every container removal safe.

1. Check the command you will run

docker rm is an alias for docker container rm. It removes containers, not images. Confirm the installed client before relying on a script or a copied example:

$ docker --version
Docker version 29.8.1, build 4a63305
$ docker rm --help
Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Your build identifier can differ after an update. The useful checks are the client version and the command shape. The installed command has three relevant options: --force for a running container, --volumes for associated anonymous volumes, and --link for removing a legacy link.

Checkpoint

You have confirmed that the command is docker rm CONTAINER, and you know which client version is supplying it.

2. Identify the exact container

List all containers, including stopped ones, before removing anything. Use the ID or name from this output, rather than guessing from a partial memory of a project:

$ docker ps --all
CONTAINER ID   IMAGE          COMMAND              CREATED        STATUS                     PORTS     NAMES
9f12ab34cd56   example:1.2    "/usr/bin/example"   2 hours ago    Exited (0) 10 minutes ago            example-job

Use the full name or a sufficiently unambiguous ID in the removal command. If several containers have similar names, inspect the candidate first:

$ CONTAINER_ID='9f12ab34cd56'
$ docker inspect "$CONTAINER_ID" --format '{{.Name}} status={{.State.Status}} image={{.Config.Image}}'
/example-job status=exited image=example:1.2

Replace the placeholder with an ID or name from your own docker ps --all output. docker inspect is read-only, so it is a useful pause point when several containers are present. Do not use docker system prune as a shortcut: that is a broader cleanup operation and can remove other unused Docker resources.

3. Remove a stopped container

Removing a stopped container is the normal case. This changes Docker state and is not reversible through docker rm, so check the ID again immediately before pressing Enter:

$ docker rm "$CONTAINER_ID"
9f12ab34cd56

Docker prints the removed container's name or ID. The image named example:1.2 remains available; only the container and its writable container layer are gone. Files written only into that layer cannot be recovered by recreating the container.

Verify that the container no longer appears:

$ docker ps --all --no-trunc --filter "id=$CONTAINER_ID"
CONTAINER ID   IMAGE   COMMAND   CREATED   STATUS   PORTS   NAMES

An empty result below the headings is the expected result. If the command reports that the container is still present, stop and re-check that you used the intended ID and Docker context.

4. Stop a running container before removal

A running container normally cannot be removed without an explicit force request. If the process is part of a service, stopping it can interrupt users or queued work. Check its status, notify anyone responsible for it, and prefer a graceful stop:

$ docker ps --filter "id=$CONTAINER_ID"
CONTAINER ID   IMAGE          COMMAND              CREATED        STATUS         PORTS     NAMES
9f12ab34cd56   example:1.2    "/usr/bin/example"   2 hours ago    Up 2 hours               example-job
$ docker stop "$CONTAINER_ID"
9f12ab34cd56
$ docker rm "$CONTAINER_ID"
9f12ab34cd56

docker stop gives the main process time to exit according to Docker's stop behaviour. If the application needs a longer drain period, arrange that with the service owner before removing the container. The recovery path is to recreate the container from its original image and configuration, or to start the service's normal deployment command. Keep that configuration until you have confirmed the replacement works.

5. Use force only for an explicit reason

Warning

Force removal of a running container sends SIGKILL to its main process before removing it. The process cannot perform a graceful shutdown, flush application state or finish an in-flight operation. Use it for a stuck or disposable container when the interruption is understood:

$ docker rm --force "$CONTAINER_ID"
9f12ab34cd56

Do not add --force simply to silence an error. First check whether the container is a production workload, whether its data is external to the container, and whether a normal docker stop is possible. There is no undo for the killed process or the removed container.

6. Decide what happens to volumes

By default, removing a container does not remove its volumes. That is often the safer default, but it can leave anonymous volumes behind. Add --volumes only when you have identified the data and accept its deletion:

$ docker rm --volumes "$CONTAINER_ID"
9f12ab34cd56

This option removes anonymous volumes associated with the container. A named volume, such as app-data, is not removed merely because it is attached to the container. Remove a named volume separately only after checking its contents and confirming that no other container needs it.

If the container has already been removed without --volumes, inspect volumes before doing further cleanup. An anonymous volume may be the only copy of useful application data. There is no general recovery command for a volume that has been deleted, so export or back up data before removing it.

7. Remove several known containers

You can pass more than one exact ID or name. Review the full list first, because one typo or broad name choice can turn a small cleanup into a larger outage:

$ docker rm example-job old-worker
example-job
old-worker

For automated cleanup, generate candidates from a deliberately narrow filter and review them before removal. An empty candidate list should not be treated as an error by a script unless your workflow requires at least one match:

$ docker ps --all --filter 'status=exited' --filter 'label=cleanup=true' --quiet
9f12ab34cd56
$ docker rm 9f12ab34cd56

Do not paste an unreviewed command substitution into a destructive command. If a shell script must remove candidates automatically, log the IDs, use restrictive filters, and keep volume removal as a separate, deliberate decision.

--link removes a specified legacy link, for example /webapp/redis, rather than removing a container. It applies to links on Docker's default bridge network, not links used with user-defined networks:

$ docker rm --link /webapp/redis
/webapp/redis

Most current applications should use user-defined networks and service discovery instead of legacy links. If you are unsure whether a link is still needed, inspect the network and application configuration before removing it. This option can stop communication between the linked containers even though the containers remain.

Done means

  • You checked the installed Docker CE CLI version and selected an exact container ID or name.
  • You inspected the container before making the change and understood whether it was running.
  • You used docker stop before removal where graceful shutdown was possible.
  • You reserved --force for a deliberate, understood interruption.
  • You treated anonymous volume deletion as a separate data-loss decision.
  • You verified that the intended container no longer appears in docker ps --all.