In a previous post, “How To Remove Multiple Docker Images”, we looked at how to remove multiple Docker images using the command line.

There, we would remove multiple Docker images like this:

docker rm 24fab8b8f344 2ce96c2e6070 4ff3513d4576

Or, more efficiently, like this:

docker rmi 24 2c 4f

The problem with this approach is that you need to know the image IDs in advance.

There is a simpler way to do this using some command-line kung fu.

Run the following command:

docker images -f dangling=true -q | xargs docker rmi -f | xargs docker rmi -f

A bunch of things are happening here:

docker images -f dangling=true -q

This lists all the dangling images. and the -q flag outputs only the IDs.

|

This pipes each of the results of the first command into the second command.

xargs

This takes the input from the pipe and converts it into command-line arguments for a subsequent command.

docker rmi -f

This removes the image associated with the specified ID.

dockerRemoveImages

TLDR

You can remove dangling Docker images with a single command.

Happy hacking!