Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete docker images older than X and not in use

Tags:

docker

I'm running out of disk space on a server and docker images shows some containers from 6 months ago but as old as 2 years ago. I'd like to remove all the ones older than 8 months. What magic can I add to docker rmi $(MAGIC) that'll accomplish this?

like image 876
Webnet Avatar asked Aug 15 '19 22:08

Webnet


People also ask

How do I delete an unused docker container?

Use the docker container prune command to remove all stopped containers, or refer to the docker system prune command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.


1 Answers

You can use docker images prune which will delete all images that are not being used by any container, combining it with filter makes you able to delete images with certain conditions, according to this docs where it says:

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"

In case you need to delete images older than 8 months the command would be:

$ docker image prune -a --filter "until=5840h"

Update: A more flexible version of the command above in case you need to change the value of until. Given that 1 month equals to 730 hour approximately and we need to delete images older than 8 months then we can use the command as the following and let the bash do the math:

$ docker image prune -a --filter "until=$((8 * 730))h"
like image 105
Mostafa Hussein Avatar answered Sep 24 '22 21:09

Mostafa Hussein