Remove All Docker Images

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: August 3, 2023

The short answer

To remove (delete) all locally installed Docker images from your machine, you can use a combination of the  docker rmi  and the  docker images  commands as follows:

Bash
$ docker rmi $(docker images -a -q)

Here, the  docker images  command is used to show the list of all locally installed images. The  -a   flag (short for  --all )  is used to list all images, including the intermediate images that are hidden by default. The  -q   flag (short for  --quiet)  is used to only show the image ID.

The  docker rmi  command, on the other hand, is used to delete one or more specific images.

Here is an example of what this command may look like right before being executed by the shell:

Bash
$ docker rmi 0c717bfd9ec0 3e4394f6b72f a8780b506fa4

Note that if one or more containers are still using any of the images you are trying to delete, Docker will output an error message saying that the image is in use and cannot be deleted. In this case, you will need to stop and remove those containers first before you can delete all images (see section below).

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering  docker remove all images  in the AI Command Search will prompt a  docker  command that can then quickly be inserted into your shell by doing  CMD+ENTER .

Remove unused images only

To delete all dangling images, which means images that have neither a repository nor a name tag, you can use the following  docker   command:

Bash
$ docker image prune

To delete all unused images (i.e. not referenced by any container) including dangling ones, you can use the  -a   flag (short for  --all ) as follows:

Bash
$ docker image prune -a

Remove all images and containers at once

To remove all images and containers on your local machine, you must first stop any currently running container using a combination of the  docker stop  command and the  docker ps  command:

Bash
$ docker stop $(docker ps -q)

Where  docker stop  will stop running containers and  docker ps -q  will list the ID of all running containers.

Once all containers are stopped, you can remove them using the  docker rm  command:

Bash
$ docker rm $(docker ps -a -q)

Where the  -a  flag of the  docker ps   command is used to list all containers including stopped ones. See our post on removing stopped Docker containers for more details.

Finally, you can remove all locally installed images using the  docker rmi  command:

Bash
$ docker rmi $(docker images -aq)

The difference between docker rmi  and docker image prune

While both commands are used to deal with the removal of Docker images, they have different functionalities.

The  docker rmi   command is used to delete one or more specific Docker images based on their name or identifier.

The  docker image prune  command, on the other hand, is used to clean up all unused Docker images including dangling ones.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container