Tips&Tricks

How to delete all Docker images and containers

docker
#!/bin/bash
# Stop all containers
docker stop $(docker ps -a -q)
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)

First of all we need to get a list of all containers. We can get it by the command:

docker ps -a -q

— all and quiet.

Now we need to stop all running containers:

docker stop $(docker ps -a -q)

It’s important because we can’t remove running container. So now we can:

docker rm $(docker ps -a -q)

And after all we can delete images of this containers:

docker rmi $(docker images -q)

That’s all!