Logo menu close
Contact Me

Remove all local docker images/volumes/networks

Use a combination of commands of the docker CLI to remove all docker images on your machine

Removing all docker images locally stored in your machines is sometimes necessary to clear up disk space.

There isn’t a single command to do this, but we can use a combination of commands using the docker CLI.

TL;DR

$ docker rmi $(docker image ls -aq)

Explanation

First, let’s list all images.

$ docker image ls

REPOSITORY                                              TAG                            IMAGE ID       CREATED         SIZE
redis                                                   6-alpine                       1ee39d9aeba4   4 months ago    30.7MB
eclipse-mosquitto                                       1.6                            9bc6c8a979e9   13 months ago   11.2MB
mongo                                                   latest                         372eb238b2a0   3 weeks ago     723MB
postgres                                                14                             347a13f1cf3e   8 months ago    430MB

Let’s check the help docs for this command.

docker image ls --help

Usage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

We can use the -a flag to list all the images and the -q flag to list the images.

docker image ls -aq

1ee39d9aeba4
9bc6c8a979e9
372eb238b2a0
347a13f1cf3e

In order to delete an image, we can use the image rm or rmi commands to delete an image. This command accepts multiple images. So technically, we can do the following.

docker rmi 1ee39d9aeba4 9bc6c8a979e9 ...

But we can pipe the output of docker image ls -aq to the rmi command to clear all images in one shot.

docker rmi $(docker image ls -aq)
Deleted: sha256:41ccdc16102cc4d823c64c6e14066c5439faa3ad0ab1f55008e80b8b54e10219
Deleted: sha256:09d9942096ab9c7cafd8d2c4b3344a1393fdd072266299afd70165c0538c37ff
Deleted: sha256:ee079f5c87fcb7fad53bea7ad04b45d60a937a26817f12016fdf841a0c15ee1c
......

Similarly for docker volumes, you can run the following.

$ docker volume rm $(docker volume ls -aq)


<- Back to blog