Skip to content

Instantly share code, notes, and snippets.

@cdracars
Created November 21, 2016 17:39
Show Gist options
  • Save cdracars/01660c990ee76be1c0a60ed615782932 to your computer and use it in GitHub Desktop.
Save cdracars/01660c990ee76be1c0a60ed615782932 to your computer and use it in GitHub Desktop.
Docker Cleanup
#!/bin/sh -e
# Delete all stopped containers (including data-only containers).
docker ps -a -q --no-trunc --filter "status=exited" | xargs --no-run-if-empty docker rm -v
# Delete all tagged images more than a month old
# (will fail to remove images still used).
docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' | grep ' months' | awk '{ print $1 }' | xargs --no-run-if-empty docker rmi || true
# Delete all 'untagged/dangling' (<none>) images
# Those are used for Docker caching mechanism.
docker images -q --no-trunc --filter dangling=true | xargs --no-run-if-empty docker rmi
# Delete all dangling volumes.
docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment