
Docker makes it easy to wrap your applications and services in containers 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 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 disk space and keeping your system organized by removing unused Docker images, containers, and volumes.
How to Use This Guide:
Key Takeaways:
docker rm, docker rmi, and docker prune that help remove unused containers, images, volumes, and networks to free disk space.docker system prune command provides a fast way to clean up stopped containers, unused networks, and dangling images, with optional flags for more thorough Docker resource removal.docker rm, docker rmi, and Docker prune commands helps avoid accidental data loss while keeping Docker environments clean and optimized.Note: The command substitution syntax, command $(command), used in the commands is available in many popular shells, such as bash, zsh, and Windows Powershell.
If you want a 1-click way to deploy a Docker application to a live server, take a look at DigitalOcean App Platform.
Docker provides a single command that will clean up any unused resources — stopped containers, unused networks, and dangling images:
- docker system prune
By default, docker system prune does not remove any volumes. To remove unused volumes, you must explicitly add the --volumes flag.
To additionally remove all stopped containers and all unused images (not just dangling images), add the -a flag to the command:
- docker system prune -a
Use the docker images command with the -a flag to locate the ID of the images you want to remove. This will show you every image, including intermediate image layers. When you’ve located the images you want to delete, you can pass their ID or tag to docker rmi:
List:
- docker images -a
Remove:
- docker rmi Image Image
Note: The -a or --all flag in the docker images command displays all the Docker images, including intermediate ones that are not referenced by any tags. By default, docker images shows only the images with at least one tag. However, there may be some images without any tags that are still taking up disk space on the system. The -a flag can be helpful in identifying images that can be pruned to save disk space. When used with the docker rmi command, the -f or --force flag can also be used to remove images with no tags.
Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose 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’re sure you want to delete them, you can use the docker image prune command:
Note: If you build an image without tagging it, the image will appear on the list of dangling images because it has no association with a tagged image. You can avoid this situation by providing a tag when you build, and you can retroactively tag an image with the docker tag command.
List:
- docker images -f dangling=true
Remove:
- docker image prune
You can find all the images that match a pattern using a combination of docker images and grep. Once you’re satisfied, you can delete them by 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 images -a | grep "pattern"
Remove:
- docker images -a | grep "pattern" | awk '{print $1":"$2}' | xargs docker rmi
All the Docker images on a system can be listed by adding -a to the docker images command. Once you’re sure 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 rmi $(docker images -a -q)
Cleaning up Docker build cache: Use the docker builder prune command to remove unused BuildKit cache data, which can consume significant disk space in modern Docker workflows.
Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove:
List:
- docker ps -a
Remove:
- docker rm ID_or_Name ID_or_Name
If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits:
Run and Remove:
- docker run --rm image_name
You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you’ve verified you want to remove those containers, use -q to pass the IDs to the docker rm command:
List:
- docker ps -a -f status=exited
Remove:
- docker rm $(docker ps -a -f status=exited -q)
Docker filters can be combined by repeating the filter flag. When you use different filter keys (for example, status and name), Docker returns containers that match all of those criteria (logical AND). When you repeat the same filter key (such as status) with multiple values, Docker returns containers that match any of those values (logical OR). For example, if you want to delete all containers marked as created (a state which can result when you run a container with an invalid command) or exited, you can use two status filters:
List:
- docker ps -a -f status=exited -f status=created
Remove:
- docker rm $(docker ps -a -f status=exited -f status=created -q)
You can find all the containers that match a pattern using a combination of docker ps and grep. When you’re satisfied that you have the list you want to delete, you can use awk and xargs to supply 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 | grep "pattern" | awk '{print $1}' | xargs docker rm
You can review the containers on your system with docker ps. Adding the -a flag will show all containers. When you’re sure you want to delete them, you can add the -q flag to supply the IDs to the docker stop and docker rm commands:
List:
- docker ps -a
Remove:
- docker stop $(docker ps -a -q)
- docker rm $(docker ps -a -q)
Use the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command:
List:
- docker volume ls
Remove:
- docker volume rm volume_name volume_name
Since volumes are designed to exist independently of containers, when a container is removed, a volume is not automatically removed at the same time. When a volume exists and is no longer connected to any containers, it’s called a dangling volume. To locate them to confirm you want to remove them, you can use the docker volume ls command with a filter to limit the results to dangling volumes. When you’re satisfied with the list, you can remove them all with docker volume prune:
List:
- docker volume ls -f dangling=true
Remove:
- docker volume prune
If you create 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 removed, its ID is displayed. Docker does not explicitly reference the removal of the volume. If it is unnamed, it is silently removed from the system. If it is named, it silently stays present.
Remove:
- docker rm -v container_name
docker rm vs docker rmi vs docker prune| Command | Description | Targets | Flags |
|---|---|---|---|
docker rm |
Removes one or more containers | Containers | -f to force removal, -v to remove volumes |
docker rmi |
Removes one or more images | Images | -f to force removal |
docker prune |
Removes unused or dangling resources | Images, Containers, Volumes, Networks | -a to remove all unused resources, -f to force removal |
Note: The -f flag is used to force the removal of resources without prompting for confirmation. The -a flag is used to remove all unused resources, including dangling ones.
When multiple containers share volumes, thread synchronization issues can arise, potentially leading to data corruption or unexpected behavior. To handle these issues, you can use the following strategies:
Use Named Volumes: Named volumes provide better control and management over shared data. Here’s an example of how to use named volumes in a Docker Compose file:
version: '3.8'
services:
app:
image: myapp
volumes:
- myvolume:/app/node_modules
volumes:
myvolume:
Implement File Locks: Use file locking mechanisms to ensure that only one container can access a file at a time. This can be achieved using tools like flock or lockfile within your application code.
Use Docker Compose: Docker Compose allows you to define and manage multi-container applications, ensuring proper synchronization and volume sharing. Here’s an example of a Docker Compose file that defines a service with a named volume:
version: '3.8'
services:
app:
image: myapp
volumes:
- myvolume:/app/node_modules
depends_on:
- db
db:
image: mydb
volumes:
- myvolume:/var/lib/mysql
volumes:
myvolume:
Excess image layers can lead to performance bottlenecks, especially during the build and deployment process. To debug and resolve these issues, follow these steps:
docker history <image> command to inspect the layers of an image and identify unnecessary layers. For example:- docker history myapp
RUN instructions into a single instruction to reduce the number of layers. For instance, instead of:RUN apt update && apt install -y python3
RUN apt install -y python3-pip
Use:
RUN apt update && apt install -y python3 python3-pip
FROM scratch AS base
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
FROM base AS builder
RUN go build -o myapp
FROM scratch
COPY --from=builder /app/myapp .
CMD ["./myapp"]
container is running errors when attempting to remove an active containerWhen you try to remove an active container, you may encounter the container is running error. To fix this issue, you have several options:
First stop the container, then remove it:
- docker stop <container_id>
- docker rm <container_id>
Force remove the container in a single command:
- docker rm -f <container_id>
Stop and remove all containers:
- docker stop $(docker ps -a -q)
- docker rm $(docker ps -a -q)
Use Docker Compose to stop and remove containers:
- docker compose down
If you’re using Docker Desktop, you can also use the GUI to stop and remove containers by right-clicking on the running container and selecting “Stop” and then “Remove”.
To delete all stopped containers in Docker, use the following command:
- docker compose down
This command will stop and remove all containers defined in your docker-compose.yml file and does not affect any other stopped containers. If you want to remove every stopped container, use:
- docker container prune
When you run docker system prune, Docker will remove all stopped containers and all networks not used by at least one container. Additionally, if you use the -a flag, Docker will also remove all unused images. This command is useful for freeing up disk space and cleaning up your Docker environment.
Yes, you can remove a running Docker container using the -f flag with the docker rm command. This will force the removal of the container without stopping it first. Here’s an example:
- docker rm -f <container_id>
To free up disk space used by Docker, you can use the following commands:
docker system prune -a to remove all unused images.docker system prune -a -v to remove all unused images and volumes.docker volume prune -a to remove all unused volumes.docker network prune -a to remove all unused networks.docker rm is used to remove a container, while docker rmi is used to remove an image. docker rm will delete a container and its associated resources, but it will not delete the image that the container was based on. docker rmi, on the other hand, will delete an image, but it will not delete any containers that are based on that image.
To completely remove a Docker image, use the following command:
- docker rmi <image-id>
Replace <image-id> with the ID or name of the image. If the image is in use by a container, you must first remove the container before removing the image.
Unused images (dangling and untagged) can be removed using the following command:
- docker image prune
To remove all unused images, use the --all flag:
- docker image prune --all
To remove all Docker images, containers, volumes, and networks, use this command:
- docker system prune --all --volumes
Note: This command will delete everything related to Docker, including all stopped containers and volumes.
You cannot directly modify a Docker image. Instead, create a new image without the unwanted files. Here’s how:
Start and login inside the container from the image:
- docker run -it <image-id> /bin/bash
Now, remove files within the container as needed.
Next, commit the changes to a new image:
- docker commit <container-id> <new-image-name>
Note: Using docker commit is generally discouraged for production workflows. The recommended approach is to update the Dockerfile to exclude unwanted files and rebuild the image. docker commit should be used only for quick experiments or debugging.
To remove containers that have been inactive for a specified time, use the following:
- docker ps -a --filter "status=exited" --filter "status=created"
- docker rm $(docker ps -a -q --filter "status=exited" --filter "status=created")
This removes containers with exited or created status. Adjust the filter based on your needs.
Docker keeps its images and container data in different places depending on the operating system and backend in use.
Linux:
On Linux systems, Docker stores all image layers, containers, and related metadata under a single directory by default:
/var/lib/docker
The internal layout of this directory varies based on the storage driver configured on the host, such as overlay2 or containerd.
macOS:
When using Docker Desktop on macOS, Linux containers and images are stored inside a virtual disk file rather than directly on the host filesystem. This file is typically found at:
~/Library/Containers/com.docker.docker/Data/vms/0/Docker.raw
You can inspect or change where this disk image is stored from the Docker Desktop interface by navigating to: Settings -> Resources -> Advanced -> Disk image location
Windows:
On Windows, Docker’s storage path depends on the container mode and backend:
Linux containers with WSL 2: Data is stored within the user profile at:
%USERPROFILE%\AppData\Local\Docker\wsl\data
Windows containers (windowsfilter driver): Image and container data reside under:
C:\ProgramData\docker\image
C:\ProgramData\docker\windowsfilter
Hyper-V backend (legacy): The storage directory is configurable during installation using the --hyper-v-default-data-root installer option.
You can verify the storage driver and path using:
- docker info | grep "Docker Root Dir"
To automatically remove a container after it exits, use the --rm flag when starting the container:
- docker run --rm <image-id>
This ensures the container is removed as soon as it stops.
This guide covers some of the common commands used to remove images, containers, and volumes with Docker. There are many other combinations and flags that can be used with each. For a comprehensive guide to what’s available, see the Docker documentation for docker system prune, docker rmi, docker rm, and docker volume rm. If there are common cleanup tasks you’d like to see in the guide, please ask or make suggestions in the comments.
For a detailed look at the different components of a Docker container, check out The Docker Ecosystem: An Introduction to Common Components. Additionally, explore more Docker-related topics with these tutorials:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix
With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
There is -q missing in Remove containers using more than one filter
should be docker rm $(docker ps -a -f status=exited -f status=created -q)
Super handy article!
I think the “Removing images according to a pattern” and “Remove containers according to a pattern” are mixed up.
For images:
docker images | grep "pattern" | awk '{print $3}' | xargs docker rmi
and alternatively, I found this one to work better (as multiple tags can link to a same image):
docker images | grep "pattern" | awk '{print $1":"$2}' | xargs docker rmi
For containers:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
Thanks for this detailed useful article!
For now as a punch line you can add the new alternate way : docker system prune
See https://docs.docker.com/engine/reference/commandline/system_prune/
Can’t figure out since this new command released.
What about the build in CLI tools
docker system prune
docker volume prune
docker network prune
And you are done!
Thanks for the document.
To remove dangling images the command seems to be docker image prune, not docker images purge
λ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
[...]
Nice… A quick read for the essential cmds…
An add up, use xargs with *-r * flag. Otherwise if left hand side doesn’t give any data which will trigger error
-r, --no-run-if-empty if there are no arguments, then do not run COMMAND; if this option is not given, COMMAND will be run at least once
docker images -a | grep “pattern” | awk ‘{print $3}’ | xargs -r docker rmi
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.