Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@lopezator

This comment has been minimized.

Copy link

lopezator commented Oct 19, 2016

Every of these should be included as a native command in docker. Great!

@lolmaus

This comment has been minimized.

Copy link

lolmaus commented Nov 16, 2016

Thank you.

@davecremins

This comment has been minimized.

Copy link

davecremins commented Feb 1, 2017

Great stuff

@JohnVonNeumann

This comment has been minimized.

Copy link

JohnVonNeumann commented May 16, 2017

Too clean. ty

@abdidarmawan007

This comment has been minimized.

Copy link

abdidarmawan007 commented May 24, 2017

thanks

@aaryno

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

DonatasD commented Jul 18, 2017

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

This comment has been minimized.

Copy link

MatthewBooth commented Dec 20, 2017

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

@petegore

This comment has been minimized.

Copy link

petegore commented Jan 21, 2018

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

@rbravo86

This comment has been minimized.

Copy link

rbravo86 commented Feb 8, 2018

Great script! Thanks!

@mnmeyers

This comment has been minimized.

Copy link

mnmeyers commented Feb 9, 2018

you da best

@tudorpavel

This comment has been minimized.

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

This comment has been minimized.

Copy link

sysrqbr commented Feb 16, 2018

thks!

@mauroherlein

This comment has been minimized.

Copy link

mauroherlein commented Apr 6, 2018

thanks!

@Marvinified

This comment has been minimized.

Copy link

Marvinified commented Jan 9, 2019

Thanks man

@knyga

This comment has been minimized.

Copy link

knyga commented Mar 23, 2019

Useful

@JeffBelback

This comment has been minimized.

Copy link
Owner Author

JeffBelback commented Jun 18, 2019

Updated with implementation from @aaryno. Good stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.