3

I have created a lot of images but I don't know how to delete them efficiently.

Does anyone know good command to remove the images smartly?

Thank you very much!

1
  • This question already had comprehensive answers here and here some of which were re-iterated below.
    – cam
    Feb 25, 2021 at 3:39

3 Answers 3

9

you can try docker rmi -f $(docker images -q)

Explanation:

docker images -q will list all the image id's. Then you pass all the image id's to docker rmi -f

3
  • 1
    Thank you! Your answer is what I want! I only want to remove the images but keep my container~ Feb 25, 2021 at 3:08
  • careful! you need to add `` wrap docker images -q like this docker rmi -f docker images -q
    – Peter Lee
    Feb 25, 2021 at 3:09
  • On a Mac you get: ❯ unknown shorthand flag: 'q' in -q See 'docker rmi --help'. Jun 5, 2021 at 8:53
5

The following should delete all your unused images and stopped containers:

$ docker system prune -a

Source

0
4

At first, you need to delete/stop the running container that is using your image(which one you want to remove).

  • docker ps -a: To see all the running containers in your machine.
  • docker stop <container_id>: To stop a running container.
  • docker rm <container_id>: To remove/delete a docker container(only if it stopped).
  • docker image ls: To see the list of all the available images with their tag, image id, creation time and size.
  • docker rmi <image_id>: To delete a specific image.
  • delete rmi -f <image_id>: To delete a docker image forcefully
  • docker rm -f (docker ps -a | awk '{print$1}'): To delete all the docker container available in your machine
  • docker image rm <image_name>: To delete a specific image

To remove the image, you have to remove/stop all the containers which are using it.

  • docker system prune -a: To clean the docker environment, removing all the containers and images.

N.B: For docker common commands you can see: docker essential commands

Not the answer you're looking for? Browse other questions tagged or ask your own question.