Friday, September 22, 2023
Home CONTAINERIZATION How to remove old and unused Docker Images?

How to remove old and unused Docker Images?


Do you have time in a project where you have been working for quite a long time, and you create and tag dozens of Docker images daily. But, after some time, you generally forget how many docker images you have built and tagged. Thanks God there is a way to get out of this mess. Let’s talk about how to remove old and unused Docker images.

Docker’s conservative approach to remove old and unused Docker Images

unused docker images
containers loading with lift

Docker seems to have a very conservative approach to deleting redundant objects. They are often referred to as “garbage collections” such as images, containers, volumes, and networks. Usually, Docker does not remove these objects if users do not have explicit demand.

This conservative approach has its pros and cons. On the positive side, when you somehow look for an ancient object in the project, it probably stays where it is. But this might return to you as a shortage in disk space. Therefore, Docker 1.13: PR 26108 and commit 86de7c0 to introduce a few new commands to help visualize how much space the docker daemon data is taking on disk. The commands allow us to clean up “unneeded” excess efficiently.

Prune images

Prune Basics

The docker image prune command enables you to remove unused images. By default, docker image prune only cleans up dangling images, which means not tagged and not referenced by any container. To remove dangling images:

docker image prune

Prune Options

To remove all images which are not used by existing containers, use the -a flag (for removing dangling and unused images).
Warning!!! This will remove all images without at least one container associated with them, SO be careful before using -a.

docker image prune -a

You will get a warning similar to the above also from Docker, and it will ask you whether you want to continue. So to bypass the prompt, use the -f or --force flag.

docker image prune -a --force

You can limit which images are pruned using filtering expressions with the --filter flag. For example, to only consider images created more than 24 hours ago:

docker image prune -a --filter "until=24h"

Another example, to remove all images (of course not used by existing containers) before a specific date and time:

docker image prune -a --force --filter "until=2017-01-04T00:00:00"

While using filtering expressions "label=<key>" or "label=<key>=<value>" with the --filter flag, only remove images with the specified labels. The following example removes images with the label deprecated:

docker image prune --filter="label=deprecated"

TIPSSS!!! If you are using positive filtering (testing for the existence of a label or that a label has a specific value), you can use docker image ls with the same filtering syntax to see which images match your filter. For example, to list the images in the above before deleting them:

docker image ls --filter="label=deprecated"

Finally.. and BONUS

Today we learned how to deal with our obsolete images. Now you know how to remove old and unused Docker images. Similarly, you can remove old and unused Docker containers, volumes, and networks. Even it is possible to clean up all of the objects with a single command. Find this magic command below as a BONUS. However, for details, I strongly recommend you browsing through the Docker documentation page.

docker system prune --volumes

Which commands do you use the most to remove old and unused Docker objects? Please share us in the comments.


Mustafa Gonen
DevOps engineer, helps companies to balance their needs throughout the software development life cycle, from coding and deployment to maintenance and updates by automation. He believes the beauty of diversity. Working in DevOps culture and being a part of this harmony makes him highly motivated and satisfied.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

How to query EC2 tags from within the instance?

Intro To help you manage your instances, images, and other Amazon EC2 resources, you can assign your own...

How to install Python3 on Amazon Linux 2?

Intro Python is an object-oriented programming language that is widely used. In addition to that, it's free and...

How to connect an AWS EC2 instance with FileZilla and do SFTP?

Intro EC2 is one of the essential services of Amazon Web Services (AWS), providing highly available and scalable...

How to install AWS CLI v2 on Linux?

Intro Whether you’ve been using AWS for some time or you are a novice, you may have noticed...

Recent Comments