How To Remove Docker Images, Containers, and Volumes

Introduction

Docker makes it easy to containerize your applications and services so you can run them anywhere. However, as you work with Docker, it’s also easy to accumulate an excessive number of unused images, containers, and data volumes that clutter the output and consume disk space.

Docker gives you all the tools you need to clean up your system from the command line. This cheat sheet style guide provides a quick reference to commands that are useful for freeing up disk space and keeping the system organized by deleting unused Docker images, containers, and volumes.

How to use this guide:

  • This guide is in cheat sheet format with separate command line snippets
  • .

  • Jump to any section that’s relevant to the task you’re trying to complete.

Purge all unused or hanging images

, containers

, volumes, and networks Docker provides a

single command that will clean up all resources (images, containers, volumes, and networks) that hang (not tagged or associated with a container):

  1. Docker System Pruning To

additionally remove stopped containers and all unused images (not just hanging images), add

the -a flag to the command: Docker

  1. System prune

-a Deleting

Docker

images

Delete one or more specific

images

Use the docker images command with the -a flag to locate the ID of the images you want to delete. This will show you all the images, including the intermediate image layers. When you have located the images you want to delete, you can pass their ID or tag

to docker rmi:

List

: docker images

  1. -a

Remove:

  1. docker rmi ImageImage

Delete hanging images Docker images

consist of several layers. Hanging images are layers that have no relation to any tagged image. They are no longer useful and consume disk space. They can be located by adding the filter flag -f with a value of dangling=true to the docker images command. When you are sure you want to delete them, you can use the

Docker image pruning command:

List

:

  1. docking images -f dangling=true

Remove:

Docker

  1. image pruning Deleting

images

according

to a pattern

You can find all images that match a pattern using a combination of Docker and grep images. Once you are satisfied, you can remove them using awk to pass the IDs to docker rmi. Note that these utilities are not supplied by Docker and are not necessarily available on all systems

:

List

: docker -a images | grep “pattern” Delete

:

docker images -a |

  1. grep “pattern” | awk ‘{print $3}’ | xargs docker rmi

Delete all images All

Docker images on a system can be listed by adding

  1. -a

to the docker images command. Once you are sure that you want to delete them all, you can add

the -q flag to pass the image ID to docker rmi: List: docker images -a

Remove

: docker

  1. rmi $(docker images

-a -q) Deleting containers

Remove one or more specific

containers Use the

  1. docker

ps command with the -a flag to locate the name or ID of the

containers you want to delete

:

List

: docker ps -a

Delete

: docker rm ID_or_NameID_or_Name Remove a container on exit

If you know when you’re creating a container that you won’t want to keep once you’re done, you can run docker run -rm to automatically delete it when you exit

:

Run and remove:

  1. docker

  1. run -rm image_name

Delete all

containers

with output You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exiting. To review the list of containers with output, use the -f flag to filter by status. When you have verified that you want to remove those containers, use -q to pass the identifiers to

the docker rm command:

List

: docker ps -a -f status=exited

Remove:

docker

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

Delete containers with more than one filter Docker filters can be combined by repeating the filter

flag

with

an additional value. This results in a list of containers that meet any of the conditions. For example, if you want to delete all containers marked as created (a state that can occur when you run a container with an invalid command) or exited, you can use two filters

:

List

: docker ps -a -f status=exited -f status=created

Remove:

docker

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

Delete containers according to a pattern

You can find all containers that match a pattern using a combination of docker ps and grep. When you are satisfied that you have the list you want to delete, you can use awk and xargs to provide the ID to docker rm. Note that these utilities are not supplied by Docker and are not necessarily available on

all systems:

List

: docker ps -a | grep “pattern”

Remove:

docker ps -a |

  1. grep

“pattern

  1. ” | awk ‘{print $1}’ | xargs

docker rm

Stop and remove all

containers

You can review the containers on your system with docker ps. Add the -a flag will display all containers. When you are sure you want to remove them, you can add the -q flag to provide the IDs to the

docker stop and docker rm commands:

List

: docker ps -a

Remove

: docker stop $(docker ps -a -q) docker

  1. rm $(docker ps -a -q) Deleting

volumes

Remove one or more specific volumes: Docker 1.9 and later

Use the docker volume ls command to locate the name(s) of the volume you want to delete. You can then delete one or more volumes with

the docker volume rm command:

List

: docker volume ls

Remove

:

  1. docker

volume

  1. rm volume_namevolume_name

Remove hanging volumes: Docker 1.9 and later

Since the point of volumes is to exist independently of containers, when a container is deleted, a volume is not automatically deleted at the same time. When a volume exists and is no longer connected to any container, it is called a hanging volume. To locate them and confirm that you want to delete them, you can use the docker volume ls command with a filter to limit the results to hanging volumes. When you are satisfied

with the list, you can delete them all with Docker volume pruning:

List

:

  1. coupler volume ls -f dangling=true

Remove:

  1. Docker volume pruning

Remove a container

and its

volume

If you have created an unnamed volume, it can be deleted at the same time as the container with the -v flag. Note that this only works with unnamed volumes. When the container is successfully deleted, its ID is displayed. Note that deleting the volume is not referenced. If it has no name, it is silently deleted from the system. If it is named, it remains present in silence.

Remove:

  1. docker rm -v container_name

Conclusion

This guide covers some of the common commands used to delete images, containers, and volumes with Docker. There are many other combinations and flags that can be used with each. For a complete guide to what’s available, see the Docker documentation for Docker System Prane, Docker rmi, Docker RM, and Docker Volume RM. If there are common cleaning tasks you’d like to see in the guide, ask or make suggestions in the comments.

For a detailed look at the different components of a Docker container, see The Docker Ecosystem: An Introduction to Common Components.