How to Remove All Stopped Docker Containers Quickly
Use the command
docker container prune to remove all stopped containers at once. Alternatively, you can run docker rm $(docker ps -a -q -f status=exited) to delete them manually.Syntax
The main command to remove all stopped containers is docker container prune. This command deletes all containers that are not running.
Alternatively, you can use docker rm $(docker ps -a -q -f status=exited) which removes containers filtered by their stopped status.
docker container prune: Removes all stopped containers after confirmation.docker rm: Removes containers by their IDs.docker ps -a -q -f status=exited: Lists IDs of all stopped containers.
bash
docker container prune docker rm $(docker ps -a -q -f status=exited)
Example
This example shows how to remove all stopped containers using docker container prune. It will ask for confirmation before deleting.
bash
$ docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 123abc456def 789ghi012jkl Total reclaimed space: 500MB
Output
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
123abc456def
789ghi012jkl
Total reclaimed space: 500MB
Common Pitfalls
One common mistake is trying to remove containers without filtering only stopped ones, which causes errors or deletes running containers if forced.
Another pitfall is forgetting to confirm the docker container prune prompt, so the command does nothing.
Also, using docker rm without checking if containers are stopped can fail or cause issues.
bash
docker rm $(docker ps -q) # This tries to remove all containers including running ones and will fail. # Correct way: docker rm $(docker ps -a -q -f status=exited)
Quick Reference
| Command | Description |
|---|---|
| docker container prune | Remove all stopped containers after confirmation |
| docker rm $(docker ps -a -q -f status=exited) | Remove all stopped containers by ID list |
| docker ps -a -q -f status=exited | List IDs of all stopped containers |
Key Takeaways
Use
docker container prune to safely remove all stopped containers with one command.Confirm the prompt when using
docker container prune to proceed with deletion.Filter containers by status to avoid removing running containers accidentally.
Use
docker rm $(docker ps -a -q -f status=exited) as an alternative to remove stopped containers manually.Avoid running
docker rm on all containers without filtering to prevent errors.