Docker command that purges everything

Viewed 643

Is there a single docker command that can be used to purge everything? Stop all containers if running, delete all images, delete all volumes... ect.

1 Answers

I don't think there is a single command to do that. You first need to stop all containers using

$ docker stop `docker ps -qa` > /dev/null 2>&1; ## Stop all running containers
$ docker system prune --volumes --all; ## Remove all unused docker components

Usage on prune can be found in docker official documentation.

You can still run the above 2 commands in a single line.

$ docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;

Or an alias should help

alias dockerRemoveAll="docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;"

Edit-1:
Based on @Zeitounator's comment, I've added > /dev/null 2>&1 to redirect whatever the output is to null and stderr. This still continues to remove all the docker contents even when the exit code is 1 for the stop command when there are no containers.

Also @Zeitounator's wipedocker function is even more elegant.

Related