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?
Asked
Active
Viewed 4,194 times
9

Ben
- 60,438
- 111
- 314
- 488
-
Does this answer your question? [How to remove old and unused Docker images](https://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images) – ggorlen Feb 19 '23 at 22:39
1 Answers
23
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"

Mostafa Hussein
- 11,063
- 3
- 36
- 61