Skip to content

Instantly share code, notes, and snippets.

@beeman
Created November 15, 2016 03:04
Show Gist options
  • Save beeman/aca41f3ebd2bf5efbd9d7fef09eac54d to your computer and use it in GitHub Desktop.
Save beeman/aca41f3ebd2bf5efbd9d7fef09eac54d to your computer and use it in GitHub Desktop.
Remove all from Docker
# Stop all containers
docker stop `docker ps -qa`
# Remove all containers
docker rm `docker ps -qa`
# Remove all images
docker rmi -f `docker images -qa `
# Remove all volumes
docker volume rm $(docker volume ls -qf)
# Remove all networks
docker network rm `docker network ls -q`
# Your installation should now be all fresh and clean.
# The following commands should not output any items:
# docker ps -a
# docker images -a
# docker volume ls
# The following command show only show the default networks:
# docker network ls
@abrichr
Copy link

abrichr commented May 7, 2021

Here's a script that doesn't generate any errors, including if there are no containers/images/volumes/networks:

#!/bin/bash

echo "Listing containers..."
containers=$(docker ps -qa)
echo "containers: $containers"

if [ ! -z "$containers" ]
then
    echo "Stopping containers..."
    docker stop $containers
    echo "Removing containers..."
    docker rm $containers
else
    echo "No containers found"
fi

echo "Listing images..."
images=$(docker images -qa)
echo "images: $images"

if [ ! -z "$images" ]
then
    echo "Removing images..."
    docker rmi -f $images
else
    echo "No images found"
fi

echo "Listing volumes..."
volumes=$(docker volume ls -q)
echo "volumes: $volumes"

if [ ! -z "$volumes" ]
then
    echo "Removing volumes..."
    docker volume rm $volumes
else
    echo "No volumes found"
fi

echo "Listing networks..."
networks=$(docker network ls -q)
echo "networks: $networks"

if [ ! -z "$networks" ]
then
    echo "Removing networks..."
    docker network rm $networks
else
    echo "No networks found"
fi

echo "These should not output any items:"
docker ps -a
docker images -a 
docker volume ls

echo "This should only show the default networks:"
docker network ls

@arlanram
Copy link

docker stop $(docker ps -qa)
docker system prune -a

that's all you need to remove everything

@iMonZ
Copy link

iMonZ commented Jun 7, 2021

Hey this line $(...) doesn't work with fish. Is there an alternative that works everywhere?

@altendky
Copy link

altendky commented Aug 1, 2021

Just drop the $, at least for that bit of fish. Or just run bash and then paste in the command.

@KristerV
Copy link

KristerV commented Aug 2, 2021

Here's the fish version for clarity.

docker stop (docker ps -qa);
docker rm (docker ps -qa);
docker rmi -f (docker images -qa);
docker volume rm (docker volume ls -q);
docker network rm (docker network ls -q);

@modernrockstar
Copy link

Hi, I tried docker volume rm $(docker volume ls -qf) to remove volumes but got the following message returned:

`flag needs an argument: 'f' in -f
See 'docker volume ls --help'.
"docker volume rm" requires at least 1 argument.
See 'docker volume rm --help'.

Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...]

Remove one or more volumes`

I tried this instead and successfully removed all volumes:
docker volume rm $(docker volume ls -q)

@AlbertRenshaw
Copy link

After these commands I still had volumes under docker volume ls had to run docker volume prune to remove them. Reclaimed almost half a gig!

@ShayestehHS
Copy link

thanks

@ilibilibom
Copy link

You have a typo, it should be:

docker system prune --volumes --all (note plural volumes)

Try this 👍

docker stop `docker ps -qa`
docker system prune --volume --all

This is actually plural now --volumes

@Lucky-Rathore
Copy link

thanks!!!

@lil12t
Copy link

lil12t commented Jun 7, 2023

Thanks

@atowersc
Copy link

<3

@marianomd
Copy link

This is missing:

docker builder prune

@sbrl
Copy link

sbrl commented Apr 22, 2024

** The backtick syntax should be replaced with $()

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