19.2.7.1 Rm Stopped Containers
A focused guide to Rm Stopped Containers, connecting core concepts with practical Docker and container operations.
Removing stopped containers is a routine maintenance task in Docker environments. When a container stops running — whether it exited normally, crashed, or was manually stopped — it enters a stopped state and remains on the Docker host consuming disk space. The container's writable filesystem layer, metadata, and any anonymous volumes it created persist until explicitly deleted. Over time, accumulating stopped containers fragments disk space and makes the output of docker ps -a difficult to navigate.
What a Stopped Container Is
A stopped container is any container that is not currently running. This includes containers with the following statuses:
exited: The container process completed and exited, either successfully (exit code 0) or with an error.created: The container was created withdocker createbut was never started.dead: The container was killed but could not be fully removed due to an error during teardown.
Running containers and paused containers are not stopped and cannot be removed with a plain docker rm call.
Viewing Stopped Containers Before Removing
Before removing stopped containers, you can review them:
docker ps -a
This lists all containers, including stopped ones, with their ID, image, command, creation time, status, and name.
To filter for only exited containers:
docker ps -a --filter status=exited
To filter for both exited and created:
docker ps -a --filter status=exited --filter status=created
Removing a Single Stopped Container
docker rm my_stopped_container
You can specify the container by name or ID. Partial IDs that are unambiguous also work:
docker rm a1b2c3
Removing Multiple Stopped Containers
List multiple containers in a single command:
docker rm container_one container_two container_three
Docker prints each successfully removed container name to standard output as it processes the list.
Removing All Stopped Containers with docker container prune
The most direct way to remove all stopped containers at once is:
docker container prune
Docker lists the containers that will be removed and asks for confirmation:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
a1b2c3d4e5f6
b2c3d4e5f6a1
c3d4e5f6a1b2
Total reclaimed space: 45.2MB
To skip the confirmation prompt:
docker container prune --force
The --filter flag allows pruning only containers that match certain criteria. For example, to remove only containers stopped more than 24 hours ago:
docker container prune --filter "until=24h"
Removing Stopped Containers with a Subshell
An older but still common approach uses command substitution to pass container IDs directly to docker rm:
docker rm $(docker ps -aq -f status=exited)
-a: Show all containers (not just running).-q: Show only container IDs (quiet mode).-f status=exited: Filter to exited containers only.
On Windows PowerShell:
docker rm $(docker ps -aq -f status=exited)
If there are no stopped containers, docker ps -aq returns nothing and docker rm with no arguments produces an error. Prefer docker container prune in scripts to avoid this.
Removing Anonymous Volumes Alongside Stopped Containers
Stopped containers may have anonymous volumes that are not automatically removed when the container is deleted:
docker rm -v my_stopped_container
The -v flag removes any anonymous volumes created exclusively for that container. Named volumes are not affected.
To prune both containers and their anonymous volumes:
docker container prune --volumes
Wait — docker container prune does not have a --volumes flag directly. To prune orphaned anonymous volumes separately:
docker volume prune
Removing All Unused Resources at Once
docker system prune removes stopped containers, dangling images, unused networks, and optionally anonymous volumes in a single command:
docker system prune
docker system prune --volumes
This is the most aggressive cleanup option and should be used carefully in environments where you want to preserve images or volumes for reuse.
Identifying What Is Safe to Remove
Containers that are safe to remove are ones whose output, logs, or filesystem contents you no longer need. Containers with important data written to their writable layer (not a mounted volume) will have that data permanently deleted when the container is removed. Always verify whether a container stores important state before deleting it.
To check what a container wrote to its own filesystem (distinct from mounted volumes):
docker diff my_stopped_container
This shows which files were added (A), changed (C), or deleted (D) in the container writable layer relative to its image.
Preventing Accumulation
For containers that are run as one-off tasks or scripts, the --rm flag on docker run automatically removes the container immediately after it exits:
docker run --rm alpine sh -c "echo done"
Using --rm is the standard practice for short-lived containers that produce no state you need to retain afterward, keeping the container list clean without manual pruning.