How can I delete Docker images older than a certain time period?

Pradnya Shinde
2020-02-03 23:21

 

Summary

How to delete Docker images older than a certain time period?

Instructions

This is the process you can follow for deleting the Docker tags older than a certain date:
Docker image layers are stored as separate artifacts within an “image” folder. If a layer is already in most Docker clients, it won’t get downloaded often. So search based on the manifest.json file, which is what will be changed only when that specific image/tag are downloaded/used.

  1. Using AQL: get the list of manifest.json which are older than a certain time period
  2. For example, you can use this to return the list of manifest.json that are 4 weeks old or more:
  • items.find({“name”:{“$eq”:”manifest.json”},”stat.downloaded”:{“$before”:”4w”}})
    • Note: this will also return the repo and path in the search result.
  1. Then using this REST API you can delete the Docker tag at that particular path that was returned above in the search result
  • If a layer is shared between two different tags and if one of the tag is a candidate for the delete in your case above, then the layer will NOT be deleted in the binary storage since it will be still referenced by the other tag.
  • Alternatively to the above manual method, you can also use a “cleanDockerImages” plugin.

Other relevant references:

Limiting Unique Docker Tags
Max Unique Snapshots