Skip to content

Instantly share code, notes, and snippets.

@ps2goat
Last active October 11, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ps2goat/bf06ff3592d6752e433292ec257a1fda to your computer and use it in GitHub Desktop.
Save ps2goat/bf06ff3592d6752e433292ec257a1fda to your computer and use it in GitHub Desktop.
PowerShell script to clean up docker images from a list of images. Use `docker image prune` for the built-in functionality.
docker images | Select-String -Pattern ' (?<sha>\b[a-zA-Z0-9]{12})\b ' | foreach {docker rmi --force $_.Matches.Groups[1].Value}
@ps2goat
Copy link
Author

ps2goat commented Oct 11, 2018

NOTE: you may have to run this multiple times if you have child images, depending on the order they are removed.. Child images will be removed on the first run, their parents on the second run, and so on.

--force forces removal of an image.

The regex finds a 12 char sha, with a space on either side. (this may have a collision with tags that are 12 chars; projects only have a space after and should be safe.)

If a match is found, we grab the first group's (<sha>) value, which ignores the spaces we are looking for on either side of the group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment