How to Delete Local Images in Docker

Isaac Tony Feb 15, 2024
  1. Delete Unused and Dangling Local Images in Docker
  2. Delete Specific Local Images in Docker
  3. Delete Untagged Local Images Using Filters in Docker
  4. Delete All Local Images in Docker
How to Delete Local Images in Docker

This tutorial will introduce the methods to delete local images in Docker.

Delete Unused and Dangling Local Images in Docker

Suppose that besides having untagged docker images, you also have unused images and some containers you’d also want to remove. In that case, opt for the command shown below.

$ docker system prune -a

This command will prompt with a warning letting you know that if you select yes, this command will:

  • Delete all stopped containers;
  • Delete all networks not used by at least one container;
  • Delete all images without at least one container associated with them;
  • Delete all cache.

However, if this command does not conform to what you need to accomplish, and you only want to remove:

  • Untagged or dangling images;
  • Stopped containers;
  • Dangling cache;
  • Networks not used by at least one container.

In this case, use the prune command without including the -a tag, as shown below.

$ docker system prune

Delete Specific Local Images in Docker

We may also want to remove a specific image from our local system and leave all other files intact.

In that case, we will need an image ID of those particular images that we would like deleted. We can access it through the images page in the Docker desktop application.

Accessing Image ID Using Docker Desktop

We can also access the image ID using our terminal or Docker CLI by running the below command.

isaactonyloi@DESKTOP-HV44HT6:~$ docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    c316d5a335a5   3 weeks ago   142MB

Now that we have obtained the image ID, we can remove that specific image using the docker rmi command append to images ID as shown below.

$ docker rmi c316d5a335a5

Output:

Untagged: nginx:latest
Untagged: nginx@sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Deleted: sha256:c316d5a335a5cf324b0dc83b3da82d7608724769f6454f6d9a621f3ec2534a5a
Deleted: sha256:67e568696593c33b4a15c9d81dc6f67499b8d973b88eb49b53d47bf4dbf4d187
Deleted: sha256:0f8d4e3d979c540644f248b4206cf540978166b095223bdc950628dca2e8f3f1
Deleted: sha256:5d75bfe8a7422476a495b27c8a1598d1206137631d350b8bdee13bc88f365282
Deleted: sha256:8284a9e28c625b2826efdd6160ea1ff7f710881a4a2afe1ef58a5eb51d3f919e
Deleted: sha256:89a1db9e1079b7574c1a707bc8c1fe04ff723bc71d4bca8bc48653e9a32186d2
Deleted: sha256:7d0ebbe3f5d26c1b5ec4d5dbb6fe3205d7061f9735080b0162d550530328abd6

Delete Untagged Local Images Using Filters in Docker

We can also use filters and wildcards to identify dangling images, i.e., images not associated with any container. We can locate pictures that satisfy the condition dangling=true using the filter tag -f.

The command below should list all images not associated with any container. However, please note that if there are no untagged images in your system, then the command will only return the headers.

$ docker images -a -f dangling=true
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    c316d5a335a5   3 weeks ago   142MB

Delete All Local Images in Docker

The -q tag allows us to retrieve and list all the image IDs in our local system when used alongside the -a tag.

$ docker images -a -q
54c9d81cbb44
c316d5a335a5

Using this command, we can list all the images under the docker rmi to remove all images from our system. Here is how we can nest that command to eliminate all the images.

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

Output:

Untagged: ubuntu:latest
Untagged: ubuntu@sha256:669e010b58baf5beb2836b253c1fd5768333f0d1dbcb834f7c07a4dc93f474be
Deleted: sha256:54c9d81cbb440897908abdcaa98674db83444636c300170cfd211e40a66f704f
Deleted: sha256:36ffdceb4c77bf34325fb695e64ea447f688797f2f1e3af224c29593310578d2
Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Image