Skip to content

Instantly share code, notes, and snippets.

@lonefreak
Last active December 3, 2019 20:46
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 lonefreak/504f8eb1f3111c79c16e to your computer and use it in GitHub Desktop.
Save lonefreak/504f8eb1f3111c79c16e to your computer and use it in GitHub Desktop.
Cleaning Up Old/Unused/All Docker Containers/Images
# removing old containers
# linux
docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
# macosx
docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs -L 10 docker rm
# removing all stopped containers
# linux
docker ps -a | grep 'Exit' | awk '{print $1}' | xargs --no-run-if-empty docker rm
# macosx
docker ps -a | grep 'Exit' | awk '{print $1}' | xargs -L 10 docker rm
# removing orphan tagless/nameless images
# won't remove any image other images depend on
# will show Error response from daemon: Conflict, wasn't deleted
# linux
docker images -a | grep '^<none>' | awk '{print $3}' | xargs --no-run-if-empty docker rmi
# macosx
docker images -a | grep '^<none>' | awk '{print $3}' | xargs -L 10 docker rmi
# removing tagged/named images and ALL its children
# BE CAREFUL!!!
# linux
docker images -a | grep -v "^<" | awk '{print $3}' | xargs --no-run-if-empty docker rmi
# macosx
docker images -a | grep -v "^<" | awk '{print $3}' | xargs -L 10 docker rmi
@jdunham22
Copy link

jdunham22 commented Nov 1, 2019

you want ps -a rather than -aq, otherwise grep has nothing to match, but this was still helpful, thanks

@lonefreak
Copy link
Author

Thanks for the tip. =) I'm glad it helped you

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