Achtung! This is for REALLY purging all of everything from Docker.

That means everything: Services, Images, Containers, Volumes. Totally clean. Everything.

You have been warned.

TL;DR

The goodies, in all their glory. No holds bar. Danger ahead.

# q is quiet, as in just the IDs
docker service rm $(docker service ls -q)

# a is for all
docker container stop $(docker container ls -aq)
docker container kill $(docker container ls -aq)
docker container rm --force $(docker container ls -aq)
# just aliases of the above
docker stop $(docker ps -aq)
docker kill $(docker ps -q)
docker rm --force $(docker ps -a -q)

docker image rm --force $(docker images -q)
# again, an alias
docker rmi --force $(docker images -q)

# prune all containers images, networks, and volumes - to the max
docker container prune --force
docker image prune --all --force
docker network prune --force
docker volume prune --force

# prune HARDER
docker system prune --all --force --volumes

Services

First, you've got to remove any and all services.

There may be none already - that's okay, just ignore the error about needing at least one argument.

docker service rm $(docker service ls -q)

If you don't do this, and you do have images set up as services, your containers are going to magically reappear and keep respawning faster than you can delete them.

Containers

Next the containers, the "live" instances of the images have got to go. Kill 'em dead!

docker container stop $(docker container ls -aq)
docker container kill $(docker container ls -aq)
docker container rm --force $(docker container ls -aq)

Or, if you prefer the shorthand aliases:

docker stop $(docker ps -aq)
docker kill $(docker ps -aq)
docker rm --force $(docker ps -aq)

That'll free up some disk space, yeah!!

Images

Now that there are no instances running, we can get rid of the image templates that they came from.

docker image rm --force $(docker images -q)

And the shorthand for the same thing:

docker rmi --force $(docker images -q)

Look at that smile curling up to the ears of your little hard drive...

Nix Networks and Volumes

Who cares about the networks, but be sure you don't have any data you might need on those volumes. They're about bite the dust.

docker network prune --force
docker volume prune --force

Force Prune All-The-Things!

Now, at this point everything should already be dead and gone.

However, one can never be too sure.

docker container prune --force
docker image prune --all --force
docker system prune --all --force --volumes

Beating a dead horse even deader...

I like to reboot the host and re-run the whole process just out of paranoia (or maybe for pleasure, I'm really not sure).

Totally unnecessary, but gives you a great sense of satisfaction when nothing happens afterwards.

Sanity checks and all that (no crazy things auto-reinstalling or whatever).

FIN

You've reached the end of the tape. Please rewind and, in the immortal words of the great Taylor Swift: "Play it again!"


By AJ ONeal

If you loved this and want more like it, sign up!


Did I make your day?
Buy me a coffeeBuy me a coffee  

(you can learn about the bigger picture I'm working towards on my patreon page )