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.

Did you enjoy reading this article?
Would you like to learn more about software craft in data engineering and MLOps?

Subscribe to the newsletter or add this blog to your RSS reader (does anyone still use them?) to get a notification when I publish a new essay!

Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.

Bartosz Mikulski

Bartosz Mikulski

  • Data/MLOps engineer by day
  • DevRel/copywriter by night
  • Python and data engineering trainer
  • Conference speaker
  • Contributed a chapter to the book "97 Things Every Data Engineer Should Know"
  • Twitter: @mikulskibartosz
Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.