Skip to content

Instantly share code, notes, and snippets.

@JeffBelback
Last active December 12, 2023 17:47
Show Gist options
  • Save JeffBelback/5687bb02f3618965ca8f to your computer and use it in GitHub Desktop.
Save JeffBelback/5687bb02f3618965ca8f to your computer and use it in GitHub Desktop.
Destroy all Docker Containers and Images
#!/bin/bash
# Stop all containers
containers=`docker ps -a -q`
if [ -n "$containers" ] ; then
docker stop $containers
fi
# Delete all containers
containers=`docker ps -a -q`
if [ -n "$containers" ]; then
docker rm -f -v $containers
fi
# Delete all images
images=`docker images -q -a`
if [ -n "$images" ]; then
docker rmi -f $images
fi
@davecremins
Copy link

Great stuff

@JohnVonNeumann
Copy link

Too clean. ty

@abdidarmawan007
Copy link

thanks

@aaryno
Copy link

aaryno commented Jun 28, 2017

I had issues with empty values and docker rm being unable to remove everything. This mod works for me.

#!/bin/bash
# Stop all containers
containers=`docker ps -a -q`
if [ -n "$containers" ] ; then
        docker stop $containers
fi
# Delete all containers
containers=`docker ps -a -q`
if [ -n "$containers" ]; then
        docker rm -f -v $containers
fi
# Delete all images
images=`docker images -q -a`
if [ -n "$images" ]; then
        docker rmi -f $images
fi

@praba230890
Copy link

praba230890 commented Jul 14, 2017

Delete all the images,

# Delete all images
docker rmi $(sudo docker images -q)

as

sudo docker rm $(docker ps -a -q)
command gave me the following error.

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers


@DonatasD
Copy link

As you need root to run docker you should rune these commands as:

# Stop all containers
sudo docker stop $(sudo docker ps -a -q)
# Delete all containers
sudo docker rm $(sudo docker ps -a -q)
# Delete all images
sudo docker rmi $(sudo docker images -q)

@MatthewBooth
Copy link

@DonatasD you don't need root to run docker.

@petegore
Copy link

I had to add -f option for docker rmi and everything worked fine. Thanks !

@rbravo86
Copy link

rbravo86 commented Feb 8, 2018

Great script! Thanks!

@mnmeyers
Copy link

mnmeyers commented Feb 9, 2018

you da best

@tudorpavel
Copy link

tudorpavel commented Feb 13, 2018

Not sure if it's a good idea or not, but you can also remove volumes:

docker volume rm $(docker volume ls -q)

Oh and by the way, there is also docker system prune.

@sysrqbr
Copy link

sysrqbr commented Feb 16, 2018

thks!

@mauroherlein
Copy link

thanks!

@Marvinified
Copy link

Thanks man

@knyga
Copy link

knyga commented Mar 23, 2019

Useful

@JeffBelback
Copy link
Author

Updated with implementation from @aaryno. Good stuff.

@hack3r-0m
Copy link

please add remove all volumes too

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