711

I recently started using Docker and never realized that I should use docker-compose down instead of ctrl-c or docker-compose stop to get rid of my experiments. I now have a large number of unneeded docker images locally.

Is there a flag I can run to delete all the local docker images & containers?

Something like docker rmi --all --force --all flag does not exist but I am looking for something with similar idea.

Promise Preston
  • 16,322
  • 10
  • 91
  • 108
Kimmo Hintikka
  • 10,842
  • 6
  • 30
  • 59

21 Answers21

1495

For Unix

To delete all containers including its volumes use,

docker rm -vf $(docker ps -aq)

To delete all the images,

docker rmi -f $(docker images -aq)

Remember, you should remove all the containers before removing all the images from which those containers were created.

For Windows (power shell, not cmd)

In case you are working on Windows (Powershell),

$images = docker images -a -q
foreach ($image in $images) { docker image rm $image -f }

Based on the comment from CodeSix, one liner for Windows Powershell,

docker images -a -q | % { docker image rm $_ -f }

For Windows using command line,

for /F %i in ('docker images -a -q') do docker rmi -f %i
Gulzar
  • 17,272
  • 18
  • 86
  • 144
techtabu
  • 18,136
  • 2
  • 23
  • 31
  • 10
    `unknown shorthand flag: 'a' in -a` when running `docker rmi -f $(docker images -a -q)` – Ashutosh Chamoli Feb 05 '19 at 10:06
  • 35
    @Ashutosh Chamoli: Doesn't work in CMD, works in PowerShell. – Jack Feb 26 '19 at 10:21
  • 11
    One-line variant for powershell: `docker images -a -q | % { docker image rm $_ -f }` – codestix Dec 17 '19 at 19:14
  • 14
    I must have come here 100s of times. Thanks for writing this answer! – Ajay Maity Jan 13 '20 at 11:21
  • 「docker rmi -f $(docker images -a -q) 」is real badass – Sinux Mar 16 '20 at 08:33
  • you might wanna add the powershell comment posted in your answer, because i tried this several times in CMD until I read this commentby @Jack – Mohammed Dawood Ansari Jul 28 '20 at 07:07
  • Thanks! It helped me. Please, remove the last dash in the PowerShell command. Note that is not part of the command in @CodeStix comment. The container one liner is `docker ps -a -q | % { docker rm -vf $_ }` – Titan Aug 02 '20 at 11:42
  • 2
    @Titan, thank you. Edited. I don't have way to test in windows, should have paid more attention to comment. – techtabu Aug 02 '20 at 23:08
  • Great @techtabu. BTW I'm using PowerShell Core on Manjaro. – Titan Aug 05 '20 at 09:14
  • Just optimization: in Windows we can combine it in PowerShell like: "docker image rm $(docker images -a -q)" – Alex Vovchuk Aug 13 '20 at 13:49
  • 1
    Great answer. Thank you – sh1hab Oct 07 '20 at 18:22
  • I kept getting `Got permission denied while trying to connect to the Docker daemon socket` so I first fixed this by using `sudo chmod 666 /var/run/docker.sock` then those commands were working perfectly. – DataPlug Feb 24 '22 at 23:42
  • I have faced an issue with AWS EC2 Instance. That instance default user is ```ubuntu```. So when I run the code that gives permission error. Then I used ```sudo docker rm -vf $(sudo docker ps -aq)``` and ```sudo docker rmi -f $(sudo docker images -aq)``` – Md Jewele Islam Mar 08 '22 at 22:29
399

Use this to delete everything:

docker system prune -a --volumes

Remove all unused containers, volumes, networks and images

WARNING! This will remove:
    - all stopped containers
    - all networks not used by at least one container
    - all volumes not used by at least one container
    - all images without at least one container associated to them
    - all build cache

https://docs.docker.com/engine/reference/commandline/system_prune/#extended-description

Robert
  • 29,597
  • 6
  • 79
  • 88
45

Here is short and quick solution I used

Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):

docker system prune

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

docker system prune -a


For more details visit link

Suhas_Pote
  • 2,539
  • 1
  • 17
  • 30
34

docker image prune -a

Remove all unused images, not just dangling ones. Add -f option to force.

Local docker version: 17.09.0-ce, Git commit: afdb6d4, OS/Arch: darwin/amd64

$ docker image prune -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
      --help            Print usage
Robert Ranjan
  • 1,174
  • 1
  • 16
  • 16
16

Easy and handy commands

To delete all images

docker rmi $(docker images -a)

To delete containers which are in exited state

docker rm $(docker ps -a -f status=exited -q)

To delete containers which are in created state

docker rm $(docker ps -a -f status=created -q)

NOTE: Remove all the containers then remove the images

Deep Nirmal
  • 1,031
  • 12
  • 12
8

If you need to delete without invoking docker:

rm -rf /var/lib/docker

This directly removes all docker images/containers/volumes from the filesystem.

VasiliNovikov
  • 8,823
  • 2
  • 43
  • 56
6

For Linux Ubuntu user, below worked for me. Word of Caution- It will remove all by the way.

For removing containers along with volumes associated with it, use below:

sudo docker rm -vf $(sudo docker ps -a -q)

For Removing images use below:

sudo docker rmi -f $(sudo docker images -a -q)
hassan
  • 7,484
  • 2
  • 23
  • 33
Sajin Pattath
  • 151
  • 2
  • 3
5

List images

docker image ls

Remove unused images

docker image prune --all

To additionally remove any stopped containers and all unused images

docker system prune -a

know more

MD SHAYON
  • 7,911
  • 47
  • 35
4

To delete all images:

docker rmi -f $(docker images -a | awk {'print $3'})

Explanation:

docker images -a | awk {'print $3'}

This command will return all image id's and then used to delete image using its id.

PalFS
  • 539
  • 5
  • 14
4

There is a bug in Windows where disk space is not reclaimed after removing the images. Rebooting Docker / Windows did not work.

In case you are using Docker Desktop, the following worked for me. Go to Troubleshoot -> Clean / purge data. This can save you a lot of disk space, maybe more than you wanted.

enter image description here

Please note: this removes everything, so think twice before doing this!

Guido
  • 5,244
  • 1
  • 25
  • 46
3

To delete all images :

docker rmi $(docker images -a -q)

where -a is all, and -q is return only image ids

To remove unused images, and containers :

docker system prune

beware as if you are using docker swarm, and your local machine is joining remote swarm (as manager/worker), your local will be the deployed repo. executing this thus removes the deployed images.

mirageglobe
  • 1,723
  • 19
  • 22
3
docker rmi $(docker images -q) --force
sdgfsdh
  • 28,918
  • 18
  • 108
  • 208
simencore
  • 31
  • 2
  • 4
    You should add some explanation when leaving an answer on a post, so that others finding it later can understand it. – Morphyish Aug 02 '19 at 18:48
2

You can try like this:

docker system prune
Bablu Ahmed
  • 3,692
  • 3
  • 36
  • 57
  • In my case this would remove a volume that I still use. Don't use this if you don't know what it does – Zach Smith Oct 30 '19 at 12:02
  • 'awk' is not recognized as an internal or external command, operable program or batch file. – Roee Jul 06 '21 at 13:12
2

Another way with xargs (Unix only)

docker image ls -q | xargs -I {} docker image rm -f {}
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
iamarkadyt
  • 1,980
  • 2
  • 12
  • 11
  • 1
    'xargs' is not recognized as an internal or external command, operable program or batch file. – Roee Jul 06 '21 at 13:13
2
  1. sudo docker images / docker images // list of images with id
  2. sudo docker rm image <image_id> / docker rm image <image_id>
Tomer Shetah
  • 8,103
  • 7
  • 23
  • 34
ani shaw
  • 33
  • 4
2

docker image rm -f $(docker image ls -a -q)

De Bonheur
  • 438
  • 6
  • 10
1

Adding to techtabu's accepted answer, If you're using docker on windows, you can use the following command

for /F "delims=" %A in ('docker ps -a -q') do docker rm %A

here, the command docker ps -a -q lists all the images and this list is passed to docker rm one by one

see this for more details on how this type of command format works in windows cmd.

corvo
  • 508
  • 1
  • 6
  • 17
1

To delete all Docker local Docker images follow 2 steps ::

step 1 : docker images ( list all docker images with ids )

     example :
     REPOSITORY    TAG    IMAGE ID            CREATED             SIZE
     pradip564/my  latest 31e522c6cfe4        3 months ago        915MB

step 2 : docker image rm 31e522c6cfe4 ( IMAGE ID)

      OUTPUT : image deleted
Debasis Das
  • 499
  • 3
  • 8
0

Here is the command I used and put it in a batch file to remove everything:

echo "Removing containers :" && if [ -n "$(docker container ls -aq)" ]; then docker container stop $(docker container ls -aq); docker container rm $(docker container ls -aq); fi; echo "Removing images :" && if [ -n "$(docker images -aq)" ]; then docker rmi -f $(docker images -aq); fi; echo "Removing volumes :" && if [ -n "$(docker volume ls -q)" ]; then docker volume rm $(docker volume ls -q); fi; echo "Removing networks :" && if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID|bridge|host|none' | awk '{print $1}')" ]; then docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID|bridge|host|none' | awk '{print $1}'); fi;

Raghav
  • 7,498
  • 5
  • 74
  • 97
  • That looks like a nice command but would be much easier as a human to assimilate in a shell script form and then could be versioned as well :) – god_is_love Nov 18 '21 at 05:35
0

The other answers don't seem to provide an easy way to delete just the containers with "auto-generated" names. This is my most frequent intent, so I wrote a Powershell script for it:

$containers = (docker container list -a).Split("`n") | % { [regex]::split($_, "\s+") | Select -Last 1 }
$containersToRemove = $containers | Where { ([regex]"^[a-z]+_[a-z]+$").IsMatch($_) }

# it's recommended to delete in batches, as too many at once can cause issues
$containersToRemove = $containersToRemove | Select-Object -First 30

foreach ($container in $containersToRemove) {
    # sync/wait-based version (slow)
    # docker container rm $container

    # async/background-process version (fast)
    Start-Process -FilePath docker -ArgumentList "container rm $container" -NoNewWindow
}

Take caution of course, as this script is just using a regular-expression: ^[a-z]+_[a-z]+$

So only use it if you know that the containers you care about do not use the same format (of lowercase-word, underscore, lowercase-word); or at least only run the first two lines, run echo $containersToRemove, and check the list before actually executing the deletions.

Venryx
  • 12,262
  • 10
  • 60
  • 82
0
docker rmi -f $(docker images -q)
Suraj Rao
  • 28,850
  • 10
  • 94
  • 99