{"id":2183,"date":"2023-02-02T02:47:20","date_gmt":"2023-02-02T01:47:20","guid":{"rendered":"https:\/\/akim.sissaoui.com\/en\/?p=2183"},"modified":"2023-02-02T02:57:00","modified_gmt":"2023-02-02T01:57:00","slug":"one-liner-delete-all-docker-images-in-bash","status":"publish","type":"post","link":"https:\/\/akim.sissaoui.com\/en\/informatique\/one-liner-delete-all-docker-images-in-bash\/","title":{"rendered":"One Liner: Delete all docker images in bash"},"content":{"rendered":"\n<p>Easy and simple dirty one liner to delete all images in docker. Useful in a lab setup, when you want to clean images. And good reference to learn how to make a loop out of a command output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for image in $(while read image;do echo $image | grep ago | cut -d\" \" -f3;done < <(sudo docker images));do sudo docker image rm $image;done\n<\/code><\/pre>\n\n\n\n<p>Let's clean it up and read it together, shall we ? <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for image in \n    $(\n        while read image;\n        do \n            echo $image | grep ago | cut -d\" \" -f3;\n        done \n    < <(sudo docker images))\ndo \n    sudo docker image rm $image\ndone<\/code><\/pre>\n\n\n\n<p>That clarifies a bit. So let's describe the actions. <\/p>\n\n\n\n<p>For each occurrence of variable image, execute docker image rm to delete that image. So far, quite clear<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for image in\n...\ndo\n   sudo docker image rm $image\ndone<\/code><\/pre>\n\n\n\n<p>Basically, for each image we find in a given command (we will see below), delete that image with docker image rm command. So \"in\" what shall we find the image IDs ? <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    $(\n        while read image;\n        do \n            echo $image | grep ago | cut -d\" \" -f3;\n        done \n    < <(sudo docker images))<\/code><\/pre>\n\n\n\n<p>This can be red as this: For each line you find in \"sudo docker images\", include only  lines which do contain \"ago\" (because this allow to exclude the header of the result, as each image has a time stamp finishing by \"ago\" for when it was created). Then, print the third field of the line, using \"space\" as a separator.<\/p>\n\n\n\n<p>At that stage, this will return a list of image IDs. And this is the value that will be used by the previous line to run docker image rm.<\/p>\n\n\n\n<p>That's it. Run this one liner, and all docker images will be deleted, as long as they do not feed a running container.  <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Easy and simple dirty one liner to delete all images in docker. Useful in a lab setup, when you want to clean images. And good reference to learn how to make a loop out of a command output Let&#8217;s clean it up and read it together, shall we ? That clarifies a bit. So let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[220],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/2183"}],"collection":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/comments?post=2183"}],"version-history":[{"count":5,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/2183\/revisions"}],"predecessor-version":[{"id":2190,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/2183\/revisions\/2190"}],"wp:attachment":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/media?parent=2183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/categories?post=2183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/tags?post=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}