How to remove all Docker images and containers

How to remove all Docker images and containers

Removing all Docker containers and images is simple, just run two commands:

1
2
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

Done, we have removed all containers and images. If you want to understand what happened, read the text below ;)

Containers vs. images

The first command removes all Docker containers. The second one removed all Docker images. A container is an instance of an image. Every time we run an image, Docker creates a new instance of it which is called a container.

Docker keeps them, because we may want to rerun a container. When rerun a stopped container it retains all the filesystem changes we have made during the previous run.

Removing containers

docker ps returns a table of all running containers. It is not enough for us. We want all containers, not only the running ones. That is why we used the -a parameter. docker ps -a returns all existing containers. The last parameter -q limits the output to only a list of container ids. That is the input accepted by docker rm.

What if we want to remove some of the containers, but keep a few of them? We can filter the containers using the -f parameter.

When I run docker ps -a I get the following list.

What if I want to remove all nix-based containers, but keep the others? I should add a filter -f “ancestor=nix” . Therefore the command that removes only nix-based containers looks like this: docker rm $(docker ps -a -q -f “ancestor=nix”).

It is a little bit more difficult when we want to keep nix containers and remove all other containers. There is no negation in the Docker filter, so we must use grep to filter the table:

1
docker ps -a | grep -v “nix”

This returns only rows without the text: nix, but we need a list of container ids, not the whole table of containers, so this solution is not complete. We can use awk to extract the ids from the output:

1
docker ps -a | grep -v “nix” | awk ‘NR>1 {print $1}’

The NR>1 condition is used because we want to exclude the header from the output. Now when we run docker rm $(docker ps -a | grep -v “nix” | awk ‘NR>1 {print $1}’) it will remove all non nix-based containers.

Removing images

We have removed the containers, but we still have images.

We can remove all of them using docker rmi $(docker images -q), but similarly to docker ps, the docker images command accepts filters.

Therefore, If we want to keep the latest images, we can exclude the “latest” tag from the output:

1
docker rmi $(docker images | grep -v “latest” | awk ‘NR>1 {print $3}’)

Note that the image ID is in the third column of the output, hence the difference in the awk command.


Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you watch programming live streams, check out my YouTube channel.
You can also follow me on Twitter: @mikulskibartosz

For business inquiries, send me a message on LinkedIn or Twitter.


Bartosz Mikulski
Bartosz Mikulski * data scientist / software engineer * conference speaker * organizer of School of A.I. meetups in Poznań * co-founder of Software Craftsmanship Poznan & Poznan Scala User Group