Delete all untagged Docker images

docker images -f dangling=true -q | xargs --no-run-if-empty docker rmi

June 15, 2018penguincoder

Explanation

docker images outputs all images currently available.

By specifying -f "dangling=true" we restrict the list to "dangling" images (i.e. untagged).

By specifying the -q option we make the output quiet, so it prints only the image hashes.

We pass the output (list of image hashes) to xargs, to run docker rmi for them to remove them. To avoid raising an error when there are no untagged images to remove, we use --no-run-if-empty. This way the command should always succeed unless there was an actual problem removing a Docker image.

Limitations

This only works in the GNU implementation of xargs (thanks to the --no-run-if-empty), the BSD implementation does not have an equivalent at the time of this writing.

Related one-liners

Delete all untagged Docker images

docker rmi $(docker images -f "dangling=true" -q)

April 27, 2018stefanobaghino