1164

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 an option to delete all the local Docker images and containers?

Something like docker rmi --all --force?

The --all option does not exist but I am looking for something to that effect.

7
  • Related: How to remove old and unused Docker images. Commented Apr 13, 2018 at 0:19
  • 8
    docker images purge --> will remove all your docker images Commented Oct 23, 2018 at 18:55
  • 7
    With xargs: docker image ls -q | xargs -I {} docker image rm -f {} Commented Dec 30, 2019 at 23:57
  • 2
    @muthukumarhelius I think you mean docker image prune (image is singular and it's prune instead of purge). Commented May 25, 2020 at 21:06
  • 52
    docker image prune --all --force Commented Oct 6, 2021 at 17:01

28 Answers 28

2421

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
Sign up to request clarification or add additional context in comments.

12 Comments

unknown shorthand flag: 'a' in -a when running docker rmi -f $(docker images -a -q)
@Ashutosh Chamoli: Doesn't work in CMD, works in PowerShell.
Great @techtabu. BTW I'm using PowerShell Core on Manjaro.
Just optimization: in Windows we can combine it in PowerShell like: "docker image rm $(docker images -a -q)"
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)
|
790

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

12 Comments

Doesn't actually reclaim all the disk space, however.
@lucian303 this approach does reclaim disk space. Maybe you are facing a particular issue.
I only seem to get the desired result when I do this and the steps in @techtabu 's answer
This should be the accepted answer, just claimed 280GB of free space while the accepted solution only cleaned 10GB.
This is the proper and clean answer. Still working in 2024. Remind take down the running containers or it won't remove the respective images.
|
136

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

2 Comments

That is not the corrrect answer as it leaves many docker objects behind. scroll down for the techtabu answer...
Or up. The order of answers is not invariant on SO. Here's a link: stackoverflow.com/a/44785784/6651650
61
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

Comments

23

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

2 Comments

to delete all images, should be - "docker rmi $(docker images -a -q)". -q returns just the image ids
that means it has deleted those images. Recheck once again
21

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

$ docker image 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 image 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}}'

Comments

15

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!

Comments

12

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)

Comments

8

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 a remote swarm (as manager/worker), your local will be the deployed repo. Executing this thus removes the deployed images.

2 Comments

That command would remove volume(s) as well that you may still be using.
unknown shorthand flag: 'a' in -a See 'docker rmi --help'.
6

You can try like this:

docker system prune

2 Comments

In my case this would remove a volume that I still use. Don't use this if you don't know what it does
'awk' is not recognized as an internal or external command, operable program or batch file.
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.

4 Comments

and also docker infrastructure files. I would NOT do that.
@TuncayGöncüoğlu what do you mean by "docker infrastructure files" ? The settings in /etc/docker are not affected
ERROR: failed to update bridge store for object type *bridge.networkConfiguration: open /var/lib/docker/network/files/local-kv.db: no such file or directory
@ravi docker may need to be stopped first, e.g. systemctl stop docker
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.

1 Comment

-q also only prints the ids
3
docker rmi $(docker images -q) --force

1 Comment

You should add some explanation when leaving an answer on a post, so that others finding it later can understand it.
3

Another way with xargs (Unix only)

docker image ls -q | xargs -I {} docker image rm -f {}

1 Comment

'xargs' is not recognized as an internal or external command, operable program or batch file.
2
  1. sudo docker images / docker images // list of images with id
  2. sudo docker rm image <image_id> / docker rm image <image_id>

Comments

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

Comments

2

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.

Comments

2

Robert Answer worked fine for me, but I run additional command for removing unused volumes.

I know the question is about removing images, but it seems like answers are walking to get a clean stage.

Only run next command if you really want to kill, delete, destroy and lost all data saved in the volumes by the containers applications and services.

docker volume prune --all

Comments

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.

Comments

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

Comments

1

Check docker containers volume

docker system df

Prune all images and volumes

docker prune --all

Comments

1

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;

1 Comment

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 :)
1
docker rmi -f $(docker images -q)

Comments

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.

Comments

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

Comments

0

docker rmi $(docker images -a)

To remove all the images

docker stop $(docker ps -q)

To remove all the containers

Comments

0
docker images -f dangling=true
docker image prune

Comments

-1

This command deletes all the unused images, including dangling (unreferenced) images as well as not associated with any container, from your local docker container

docker image prune --all

3 Comments

Not helpful. This results in an error: No images found matching "prune": did you mean "docker image prune". Even if it worked, prune does not delete everything as the OP asked for. It only deletes dangling images.
@starbeamrainbowlabs the question was to delet "unneeded" images so I assume @shaheer tried well. Keep it up and don't forget next time to answer with correct "OPTIONS" such docker image prune --all
Thank You @starbeamrainbowlabs. A mistake from my end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.