19.2.7 Rm CLI Command
A focused guide to Rm CLI Command, connecting core concepts with practical Docker and container operations.
docker rm removes one or more stopped containers from the Docker host. It permanently deletes the container filesystem layer, the container metadata, and any anonymous volumes created exclusively for that container. The command only operates on containers that are not currently running; attempting to remove a running container without the force flag produces an error.
Basic Usage
docker rm <container_name_or_id>
docker rm my_container
docker rm a1b2c3d4e5f6
You can use either the full container ID, a unique prefix of the ID, or the container name. Partial IDs work as long as they are unambiguous among all containers on the host.
Removing Multiple Containers
Multiple containers can be removed in a single command by listing them space-separated:
docker rm container_one container_two container_three
Docker processes the list sequentially and prints each successfully removed container name to standard output.
Flags
--force / -f
The --force flag removes a running container by sending it a SIGKILL signal before deleting it. Without this flag, docker rm refuses to remove a running container.
docker rm --force my_running_container
This is equivalent to running docker stop (with a SIGKILL instead of SIGTERM) followed by docker rm, but done atomically. Use this flag with care because it does not give the container process any time to shut down gracefully.
--volumes / -v
The --volumes flag removes any anonymous volumes attached to the container when the container itself is removed.
docker rm --volumes my_container
Without this flag, anonymous volumes created by docker run remain on the host even after the container is removed, and must be cleaned up separately with docker volume rm or docker volume prune.
Named volumes are never removed by docker rm --volumes. Named volumes are only removed explicitly with docker volume rm or docker volume prune.
--link / -l
The --link flag removes the specified link between containers rather than the container itself. Links are a legacy networking feature.
docker rm --link my_container/alias
Removing All Stopped Containers
To remove all containers that are in a stopped, created, or exited state, combine docker rm with docker ps:
docker rm $(docker ps -aq -f status=exited)
On Windows PowerShell:
docker rm $(docker ps -aq -f status=exited)
A cleaner alternative that handles the case where there are no stopped containers (avoiding an error from an empty argument list):
docker container prune
docker container prune removes all stopped containers and prompts for confirmation before proceeding.
docker container prune --force
The --force flag on prune skips the confirmation prompt.
Removing Containers and Their Anonymous Volumes Together
docker rm -v my_container
This is the recommended practice when you know you will not reuse the data in the anonymous volume, since orphaned anonymous volumes consume disk space and are not shown in normal docker volume ls output without a filter.
Error: Container is Running
If you attempt to remove a running container without --force:
docker rm my_running_container
Docker returns an error:
Error response from daemon: You cannot remove a running container a1b2c3...
Stop the container before attempting removal or force remove
Stop the container first:
docker stop my_container
docker rm my_container
Or force-remove in one step:
docker rm -f my_container
Relationship to Container Lifecycle
A container must be in a stopped state for docker rm to work without --force. The typical lifecycle is:
- Container created with
docker createordocker run. - Container running after
docker startordocker run. - Container stopped after
docker stopor process exit. - Container removed with
docker rm.
After docker rm, the container no longer appears in docker ps -a output. The image the container was created from is not affected by removing the container.
What docker rm Does Not Remove
- The image the container was based on. Use
docker rmito remove images. - Named volumes attached to the container. Use
docker volume rmordocker volume prune. - Docker networks. Use
docker network rmordocker network prune.
Automating Cleanup on Exit
When running short-lived containers (scripts, one-off tasks), use the --rm flag on docker run to automatically remove the container when it exits:
docker run --rm alpine echo "hello world"
This is equivalent to running docker rm immediately after the container exits, and is the standard practice for temporary containers that do not need to be inspected after they complete.
Checking What Will Be Removed
Before removing containers, use docker ps -a to review the list of stopped containers:
docker ps -a --filter status=exited
This shows all exited containers with their IDs, names, image, and exit time, allowing you to confirm which containers are safe to remove.