Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Docker Cleanup Scripts

Docker - How to cleanup (unused) resources

Cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see:

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

delete networks

$ docker network ls  
$ docker network ls | grep "bridge"   
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

// see:

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

// see:

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")
Copy link

johnpapa commented Aug 4, 2017

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Copy link

You could use xargs which would do nothing if no images of the type specified were found instead of showing an error.

For example:

docker ps -qa --no-trunc --filter "status=exited" | xargs docker rm

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