44

I am very new to Docker and enjoying it very much.

I want to delete all images and containers from local as well as from docker hub. Is there any single command to do that?

davnicwil
  • 28,487
  • 16
  • 107
  • 123
Mahammad Adil Azeem
  • 9,112
  • 13
  • 57
  • 84

7 Answers7

82

To remove all containers,

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

-v: Remove all associated volumes

-f: Forces the removal. Like, if any containers is running, you need -f to remove them.

To remove all images,

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

-a: for all containers, even not running, (or images)

-q: to remove all the details other than the ID of containers (or images)

Auzias
  • 3,638
  • 14
  • 36
gibumba
  • 826
  • 7
  • 3
  • 2
    WARNING: Error loading config file:/home/adil/.docker/config.json - stat /home/adil/.docker/config.json: permission denied Cannot connect to the Docker daemon. Is the docker daemon running on this host? docker: "rmi" requires a minimum of 1 argument. See 'docker rmi --help'. Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] Remove one or more images – Mahammad Adil Azeem Jun 27 '16 at 18:54
  • That looks to be an environmental issue on your end. The Docker daemon isn't running, so the nested Docker images command returns nothing, so the outer Docker rmi command fails since it's expecting at least one image name to be given. – Nauraushaun Jun 27 '16 at 22:41
  • I have seen that permission denied warnings in some Ubuntu machines. I ignored them since it never caused any troubles. Unless you have any images in your local docker, you will get 'docker "rmi" required ... " message from docker. Also, check if your docker is running via `service status docker` command. – gibumba Jun 28 '16 at 01:58
  • 2
    @gibumba This is the message i am getting after running status command ... status: unrecognized service – Mahammad Adil Azeem Jun 28 '16 at 19:04
  • sorry, it should be `service docker status` – gibumba Jun 28 '16 at 19:49
  • 2
    Guys @gibumba and others Please upvote this question. I don't understand why someone voted it down – Mahammad Adil Azeem Jun 29 '16 at 00:18
  • 3
    `unknown shorthand flag: 'a' in -a` – fabpico Nov 29 '19 at 07:19
  • 1
    unknown shorthand flag: 'a' in -a See 'docker rmi --help'. – Roee Jul 06 '21 at 13:16
  • it says docker rm -vf $(docker ps -a -q) "docker rm" requires at least 1 argument. See 'docker rm --help'. Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...] Remove one or more containers – Yuvraj Agarkar Nov 20 '22 at 08:44
  • Note: this also works on Windows, but only in Powershell (not Command Prompt) – cowlinator Jul 14 '23 at 01:32
22

This is the command to be used for your question. It deletes everything(container , images, cache, etc..)

docker system prune

Warning

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache
Are you sure you want to continue? [y/N] n
Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31
8

to delete all containers:

docker rm $(docker ps -a -q)

to delete all images:

docker rmi $(docker images -q)

Note that you can't revert and you can't kill containers which running, you should stop them before

Walterwhites
  • 1,287
  • 13
  • 9
  • 1
    Additional hint: if you are using `Windows`, run the above scripts inside `Windows PowerShell` not inside `Command Promot ` – Ahmed Nabil Oct 08 '20 at 16:56
2

See all the existing images:

docker images -a

See all the existing containers:

docker ps -a

Delete single image:

docker images -a
docker rmi <IMAGE_ID>

Delete single container:

docker ps -a
docker rm <CONTAINER_ID>

Delete multiple images:

docker images -a
docker rmi <IMAGE_ID1> <IMAGE_ID2>

Delete multiple containers:

docker ps -a
docker rm <CONTAINER_ID1> <CONTAINER_ID2>

Stop all containers and remove them:

docker rm $(docker kill $(docker ps -aq))

Delete all images:

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

Delete both all stopped containers and images in a single command:

docker rm $(docker ps -a -q) && docker rmi -f $(docker images -a -q)

To prune all containers:

docker container prune

Delete all unused data (i.e., in order: containers stopped, volumes without containers and images with no containers):

docker system prune
Md. Shahariar Hossen
  • 1,367
  • 10
  • 11
0

For me the problem were restarting containers. So I did: 1. docker stack ls 2. docker stack rm <xxxxxx> 3. docker stop $(docker ps -aq) 4. docker rm $(docker ps -aq) 5. docker system prune

mirek
  • 1,140
  • 11
  • 10
0

To delete all containers including its volumes used

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

To delete all the images, but before this you should remove all the containers which are created from this images

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

If you get permission denied, try putting sudo in front of commands as:

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

These commands are taken from this link. Please check it for a list of commands.

Sardar Faisal
  • 594
  • 5
  • 20
0

Using docker-compose it is easier:

ec2-user@devbox$ docker-compose down --rmi all

Reference: https://docs.docker.com/compose/reference/down/

Usage: docker-compose down [options]

Options:
    --rmi type              Remove images. Type must be one of:
                              'all': Remove all images used by any service.
                              'local': Remove only images that don't have a
                              custom tag set by the `image` field.
    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.
    --remove-orphans        Remove containers for services not defined in the
                            Compose file
    -t, --timeout TIMEOUT   Specify a shutdown timeout in seconds.
                            (default: 10)
Luis Estrada
  • 371
  • 7
  • 20