934

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.

Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63

27 Answers27

1972

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.

Windows - Powershell

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

Windows - cmd.exe

for /F %i in ('docker images -a -q') do docker rmi -f %i
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
techtabu
  • 23,241
  • 3
  • 25
  • 34
  • 13
    `unknown shorthand flag: 'a' in -a` when running `docker rmi -f $(docker images -a -q)` – Ashutosh Chamoli Feb 05 '19 at 10:06
  • 40
    @Ashutosh Chamoli: Doesn't work in CMD, works in PowerShell. – Jack Feb 26 '19 at 10:21
  • 12
    One-line variant for powershell: `docker images -a -q | % { docker image rm $_ -f }` – codestix Dec 17 '19 at 19:14
  • 「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 $_ }` – Eligio Mariño 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. – Eligio Mariño 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
  • 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
  • 2
    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
  • how about volumes :) – ORHAN ERDAY Oct 12 '22 at 06:34
  • If you want to just remove volumes you can do `docker volume prune`. Here is the docs. https://docs.docker.com/engine/reference/commandline/volume_prune/ – techtabu Oct 12 '22 at 15:10
  • Since the question asks about Images (as opposed to containers), I feel like the top two lines of code should be switched around (so the code for images is first) – stevec Nov 16 '22 at 07:16
  • btw, do not use quotes around the `$()` in the Unix version, otherwise all image/container ids are interpreted as a single argument – Alex M Mar 07 '23 at 16:03
536

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
  • 33,429
  • 8
  • 90
  • 94
  • 5
    Doesn't actually reclaim all the disk space, however. – lucian303 Mar 01 '19 at 00:57
  • 5
    @lucian303 this approach does reclaim disk space. Maybe you are facing a particular issue. – Robert Nov 07 '19 at 16:48
  • I only seem to get the desired result when I do this *and* the steps in @techtabu 's answer – francojposa Dec 30 '19 at 17:58
  • unknown flag: --volumes – aswzen Jul 19 '20 at 05:05
  • @lucian303 in my case the space was reclaimed only after restarting the docker – dav Mar 25 '21 at 13:42
  • Using Windows and Docker Desktop, disk space was not reclaimed. The other solutions mentioned in the comments did not work for me. I posted an answer that helped me in reclaiming disk space. – Guido Jul 01 '21 at 07:34
  • @lucian303 It *does* reclaim disk space. In my case: `Total reclaimed space: 7.575GB`. And `docker ps -a` and `docker images` empty after this. – questionto42 Dec 22 '21 at 10:35
  • 3
    This should be the accepted answer, just claimed 280GB of free space while the accepted solution only cleaned 10GB. – Alexandre Brown Mar 29 '23 at 12:45
  • It's not everything if it keeps containers in use. I have some containers that I can't seems to stop (they restart) and I have no idea where they were started from. So something that absolutely just resets Docker on a system would be the best answer imho. – Freek May 26 '23 at 09:27
  • 1
    It worked for me. It helped deleting the volumes too – Rahul Dey Jun 29 '23 at 19:15
73

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
  • 3,620
  • 1
  • 23
  • 38
50
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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Robert Ranjan
  • 1,538
  • 3
  • 19
  • 17
18

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,141
  • 1
  • 15
  • 14
13

Elimination of all unused or pending images, containers, volumes and networks

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

docker system prune

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

docker system prune -a

https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes-fr

PavanDevarakonda
  • 625
  • 5
  • 27
Denyroprez
  • 131
  • 1
  • 3
10

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
  • 6,182
  • 1
  • 29
  • 50
10

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,812
  • 2
  • 25
  • 36
Sajin Pattath
  • 241
  • 3
  • 5
6

Delete only all images then run the following command

docker rmi -f $(docker images -aq) 

Delete unused container, images and volumes then run the following command

docker system prune -a -f 
Vineet Kumar
  • 333
  • 4
  • 7
5

If you need to delete without invoking docker (for example, if docker is broken and does not start, has been removed itself but not its images, etc):

systemctl stop docker  # stop docker if it was running
rm -rf /var/lib/docker

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

VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
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
  • 731
  • 6
  • 16
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
  • 2,446
  • 2
  • 24
  • 30
3
docker rmi $(docker images -q) --force
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
arencore
  • 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
3

To remove all images without at least one container associated to them

$ docker images prune -a

To get all the names of the images : docker images -a -q and remove all images using this command in the same line.

docker image rmi $(docker images -a -q)

If you have images attached to at least one of the running containers, it is a good idea to stop them first.

To remove images created more than 10 days (240 h ) ago:

$ docker images prune -a --force --filter "until=240h"

You can verify by using the following command:

$  docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}\t{{.Size}}'
2

You can try like this:

docker system prune
Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64
  • 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
  • 179,855
  • 19
  • 132
  • 245
iamarkadyt
  • 2,740
  • 2
  • 13
  • 13
  • 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,413
  • 7
  • 27
  • 35
ani shaw
  • 33
  • 4
2

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

De Bonheur
  • 742
  • 10
  • 13
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
  • 676
  • 2
  • 7
  • 20
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
  • 549
  • 3
  • 8
1

Check docker containers volume

docker system df

Prune all images and volumes

docker prune --all

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
  • 8,772
  • 6
  • 82
  • 106
  • 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
  • 15,624
  • 10
  • 70
  • 96
0
docker rmi -f $(docker images -q)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0
docker images -f dangling=true
docker image prune
Parvinder Kumar
  • 775
  • 5
  • 8
0

To remove any stopped containers and all unused images.

docker system prune -a

To delete unused images, containers and volumes then run the following command

docker system prune -a -f

Vineet Kumar
  • 333
  • 4
  • 7
Kamran
  • 669
  • 6
  • 9
0

To remove a subset of images

Add a filter, with -f.

docker rmi -f $(docker images -af <YOUR_FILTER_PATTERN> -q)

E.g. if docker images returns:

image3                      latest    3a371a8efe91   12 days ago    987MB
image2                      latest    cca6cd42c697   12 days ago    987MB
image1                      latest    0373470f2972   12 days ago    987MB
image0                      latest    1a99848b511f   13 days ago    987MB
node                        18        5087dac9940a   2 weeks ago     947MB
nginx                       latest    8a5e3e44915c   2 weeks ago     135MB
alpine                      latest    04eeaa5f8c35   6 weeks ago     7.46MB
hello-world                 latest    46331d942d63   11 months ago   9.14kB

-f since=*

docker rmi -f $(docker images -af since=node:18 -q)

will result in:

node                        18        5087dac9940a   2 weeks ago     947MB
nginx                       latest    8a5e3e44915c   2 weeks ago     135MB
alpine                      latest    04eeaa5f8c35   6 weeks ago     7.46MB
hello-world                 latest    46331d942d63   11 months ago   9.14kB

There are a few options including since,before,label or reference(pattern match). The docs.

This might be useful if you have a development loop involving repeated builds, but want to keep the base OS local (e.g. node) to avoid repeated downloads.

Lee
  • 29,398
  • 28
  • 117
  • 170