Right way to delete unused docker images on OpenShift Origin
The half-manual way I am using is to list the images' properties and the process the list further, as follows:
oc get images -o jsonpath='{range .items[*]}{.dockerImageReference}{.dockerImageMetadata.Created} {.dockerImageMetadata.Size}{"\n"}{end}'
registry.access.redhat.com/jboss-datavirt-6/datavirt63-openshift@sha256:ed82847d159ff9f5e43520b3479a3b15919195c2dc86781cc85b84368d84a7742017-06-26T10:44:21Z 571015080
registry.access.redhat.com/jboss-eap-7/eap70-openshift@sha256:eddcc75d3e7fd5e25b9599a5cb72bd48b403c308e91c501f5dcc9c157ea86c4f2017-06-12T07:37:20Z 572990540
registry.access.redhat.com/jboss-webserver-3/webserver30-tomcat7-openshift@sha256:ee09a9ae44c338c39783d559367aef63ce548d8de973e09808c4c236c5dcb4852017-08-02T14:53:32Z 190637724
registry.access.redhat.com/jboss-webserver-3/webserver30-tomcat7-openshift@sha256:ee68b3072bfabacf1272b183c09a43b8116902ac722ff2fca3185235447a453f2017-05-10T10:48:06Z 256713194
...
Then, replace '@' to have separate columns for image 'pull name' and image id, and apply sorting by the first and the third column, so image name and date:
oc get images -o jsonpath='\
{range .items[*]}{.dockerImageReference} \
{.dockerImageMetadata.Created} \
{.dockerImageMetadata.Size}{"\n"}{end}' \
| sed 's/@/ /' \
| sort -k1,1 -k3,3
From there, I can apply a precise filter like grep -E 'myimage.*2016-'
, etc. Finally, I use awk to process the output and get oc commands:
...all the above with filters... | awk '{print "oc delete image "$2}
results in:
oc delete image sha256:2cd7c7e0443779e2a090f326d2f0daf0dbdac719e1e254e166fac5c0e107708e
oc delete image sha256:1d7e028ff3a3439de4a18dad307d5099db64f4e5a12819e7cf2ff72ee21e39d5
oc delete image sha256:9f31e9f2a18b0ea07f2c0e503e01a784e9365db485f163b6699799a4b53415cf
oc delete image sha256:dd97c061f076e2c1c8d368896a806056c9bc7d96d1065aca097d86959ce5130c
You can obviously process the list of images in a language of your choice, parsing dates and removing older duplicates, etc.
You should use the oc adm prune images
command to remove unused images. Note that this will be conservative and not delete images that have recently been changed.
See https://docs.openshift.org/latest/admin_guide/pruning_resources.html for more.