Reputation: 641
I want to remove all of my docker containers at once. I tried to use $ docker rm [container_id] to do so, but it removed only one container, not all.
Is there any way to remove all docker containers using one single line of code?
Upvotes: 50
Views: 156023
Reputation: 907
These two commands if you want to remove all container or image
for container
sudo docker rm -f $(sudo docker ps -a -q)
for images
sudo docker image remove -f $(sudo docker images -a -q)
by using -f flag we can remove the container forcefully
Upvotes: 15
Reputation: 126
I can show you an amazing trick which helps you to be a better Linux user.
by typing docker ps -a | cut -d ' ' -f1 | xargs docker rm
all containers will be removed. first, you list all docker containers, then separate all container ids then, remove all of them in one line of command.
Upvotes: 2
Reputation: 3873
You can use the following line
docker kill $(docker ps -q) || true && docker rm $(docker ps -a -q) || true && docker rmi -f $(docker images -q) || true
If you are using jenkins or Gitlb CI/CD, just running docker rm $(docker ps -a -q)
or any other relevant command would fail if there are no containers or images. With the above line, it will be prevented.
Upvotes: -1
Reputation: 2252
As from Docker 1.13.0
--- Docker API 1.25
:
docker container prune
Output:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
df226cc24539833a1c88f46bfa382ebe2e89c21805288f5e6bfc37cb7f662505
993bd58faaa22cb5dbc263eca33c8d1a241bd0a1f73b082e23e3a249fe1dfc0d
...
Total reclaimed space: 13.88MB
See more on docker container prune
There is docker versioning matrix for better understanding docker versions (current is Docker 17.12
--- Docker API 1.35
)
Upvotes: 31
Reputation: 77961
Remove containers based on status:
docker rm -v $(docker ps --filter status=exited -q)
Note:
To clean out all containers on my development machine:
docker rm -v -f $(docker ps -qa)
Note:
Upvotes: 91
Reputation: 45
Killing Active Containers:
for /F %i in ('docker ps') do docker kill %i
Remove Passive Containers:
for /F %i in ('docker ps -qa') do docker rm %i
It is works in Docker 17.xx
Upvotes: 0
Reputation: 22069
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
Upvotes: 3
Reputation: 641
i do it with a bash script loop and a docker rm command:
$ for id in $(docker ps -aq); do docker rm $id; done
Upvotes: 1