4

I know how to remove old Docker images created more that N days ago. See here
But what I'd really like to do is to remove old Docker images that were not used for the last N days.
The goal is to keep images that are frequently used, even if no container is actually using them when I do the cleanup.
Is this possible?

2 Answers2

1

This is not trivial because docker doesn't track usage timestamps, only creation. So the builtin commands can't make decisions based on usage.

An old but periodically active ticket to address this is https://github.com/moby/moby/issues/4237

I'm aware of two separate programs which attempt to address this, both monitor the docker events stream to create their own log of container usage.

docuum keeps the disk usage to a set limit, deleting the oldest image by usage if that limit is exceeded https://github.com/stepchowfun/docuum

docker-gc deletes any image that hasn't been used in X time. When the docker-gc documentation refers to age it means time since last used https://github.com/docwhat/docker-gc

lod
  • 1,098
  • 10
  • 13
0

The closest thing I can think of would be docker image prune -a --filter "until=24h", which deletes all unused images created at least 24h ago.

For further reference you can check out the Docker Pruning and Docker System Prune documentation.

Domenico Sibilio
  • 1,189
  • 1
  • 7
  • 20