How to remove all Docker images and containers

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

Table of Contents

  1. Containers vs. images
  2. Removing containers
  3. Removing images

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:

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:

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:

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.

Older post

Understanding uncertainty intervals generated by Prophet

How to tweak uncertainty intervals in Prophet.

Newer post

[book review] The effective engineer

What is the best investment of your time?

Are you looking for an experienced AI consultant? Do you need assistance with your RAG or Agentic Workflow?
Schedule a call, send me a message on LinkedIn. Schedule a call or send me a message on LinkedIn

>