How to purge Docker images, containers, networks or volumes
Purpose
The purpose of this post is to learn how to easily delete images, containers, networks and volumes from an existing Docker local (or remote) Docker installation using its CLI. A Docker installation is required to follow this tutorial. Docker version used to prepare this article: 18.04.0-ce
. Note that other Docker version commands can vary.
Note: If you need to remove images/tags directly from a remote Docker repository we have a post “Delete images from Docker Hub” explaining how to achieve that.
Delete Docker containers
Whether you need to Docker container because there is a new image version in the pipeline or it is just a test in a dev environment you can use this command to remove a specific container:
$ docker container rm [container_name / container_id]
# deleting multiple containers
$ docker container rm [container_1] [container_2] ... [container_N]
If the container o containers that you should delete are running you could stop or delete them first or use the parameter f as follows to force their deletion:
$ docker container rm -f [container_name / container_id]
Specially in development environments you could end up with dozens of stopped containers and obviously always come in handy to delete them and free disk space: Docker command to purge all stopped containers:
$ docker container prune
Finally, you can delete all stopped containers based on a filter:
$ docker container prune --filter 'NAME=VALUE'
# Example: Deletes stopped containers created until 1 minute ago.
$ docker container prune --filter 'until=1m'
Delete Docker images
Delete Docker images by name/tag or ID
To delete a Docker image you first need to know the image name and tag or the image ID. You can use the following command to list all current images:
$ docker image ls
# or
$ docker images
Once you know the name of the image name + tag or the image ID you can use this command to remove a specific Docker image:
docker image rm [image_name:tag / image_id]
# or
docker rmi [image_name:tag / image_id]
If you remove a Docker image using its name like
$ docker rmi image_name
without specifying an image tag, Docker will uselatest
as a default tag like$ docker rmi image_name:latest
.If you remove a Docker image using its ID, it is not necessary to specify the full string. Example: To delete the Docker image with ID d3d96b1e5d48 you could run
$ docker rmi image_name d3d96b
or$ docker rmi image_name d3d
. If you are using very few characters make sure you are not deleting other images as well.
Delete Docker dangling images
In addition to tagged or named Docker images, there are dangling images. These images don’t have any relationship to any current image and there aren’t used anymore. Because of that, it is a good practise to delete them and keeping more space in the disk. This is the Docker command to prune dangling images:
$ docker image prune
# or
$ docker rmi $(docker images -qf "dangling=true")
Delete Docker unused images
Finally, part of the current tagged images could be not used. Docker provides a command to purge these unused images as well (previous dangling images included):
$ docker image prune -a
Delete Docker networks
Delete Docker networks by name or id
To delete a Docker network it’s very useful to get a list of the current networks:
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
387cccf19c98 custom_net bridge local
bf5345d30d7f bridge bridge local
1ae6aef09e5c host host local
...
Once you’ve identified the Docker network to be deleted you can run this command to remove it:
$ docker network rm [network_name / network_id]
# or
$ docker network remove [network_name / network_id]
# example
$ docker network rm custom_net
# or
$ docker network rm 387cccf19c98
# or
$ docker network remove 387cc
Delete Docker unused networks
If you want to tidy up a little bit the Docker networks you could delete the unused networks:
$ docker network prune
# using --filter you can filter which unused networks you want to delete as we did before with stopped containers
$ docker container prune --filter 'NAME=VALUE'
Delete Docker networks with active endpoints
If you try to delete a busy newtork you’ll get the following error:
$ docker network rm custom_net
Error response from daemon: error while removing network: network bi_private id <ID> has active endpoints.
Probably this is because there are containers using this network and make sense to review this scenario o delete all the containers first. But if this is not the case and you really need to delete the Docker network you can run the following command and write down all the containers in the Docker net:
$ docker network inspect custom_net
...
"Containers": [..]
...
Once you have the list of Docker containers you can disconnect the network from them:
$ docker network disconnect -f custom_net container_1
$ docker network disconnect -f custom_net container_2
...
$ docker network disconnect -f custom_net container_N
And finally delete the network:
$ docker network rm custom_net
Delete Docker volumes
Docker volumes are used to provide or keep container’s configuration or to persist data from it. Either you need to remove unused volumes, the persisted data from a running container or its configuration, you can use the following commands to remove a Docker volume:
First of all you should list all current volumes:
$ docker volume ls
DRIVER VOLUME NAME
local named_volume
local 0a0bce5c74f249a9954120ce3d6cbc5fb388d1fadc27fd55c2a008d2c1bd8d1a
local 1e3f5e108a2e2542f018d0302f3d598bb90ded4cd604fed352c9530d30d35a56
...
Named volumes are defined by the user and there is no issue to identify them. Things change a little bit for auto-generated volumes. These volumes can be tricky to be identified and if you need to delete one of them from a known container you should try to locate it:
$ docker inspect containerid
...
"Mounts": [{volume 6d29ac8a196.. }{ ... }]
The volume name to be deleted is 6d29ac8a196..
Delete a specific Docker volume
$ docker volume rm [volume_name]
Delete all Docker unused volumes
$ docker volume prune
# using --filter you can filter which unused volumes you want to delete as we did before with stopped containers and networks.
$ docker volume prune --filter 'NAME=VALUE'
Deleting Docker containers, images, nets and volumes
Specially in dev environments sometimes you need to delete the full Docker installation. This command could help you to delete stopped containers, dangling images, networks and build cache at the same time:
$ docker system prune
Using -a at the end of the command will delete all unused images:
$ docker system prune -a
Finally, adding –volumes will delete unused volumes as well:
$ docker system prune -a --volumes
Previous commands will request [y/N] to confirm or not the deletion. You can add the
-f or --force
parameter to avoid prompt and perform the action right away.
Finally, you should definitely take a look at these books to fuel your Docker knowledge:
DevOps books:
Cloud providers:

DigitalOcean offers affordable pricing for VMs and many other public cloud services. You can sign up for DigitalOcean and receive a $100 free credit using this referral link.