Docker is software containerization technology that helps developers create and deploy applications across disparate platforms ranging from the desktop to the cloud.

A snapshot — or blueprint — of the source code, dependencies, and tools required to build an application within a Docker container is known as an image. Docker applications that require persistent data can rely on storage called volumes that are independent of the underlying operating system.

Effective organization of images, volumes, and containers is important when using Docker. Unused incarnations of these assets can accumulate, taking up valuable disk space and eventually affecting system performance.

This article explores different ways to maintain system organization by clearing images (both individually and all at once), volumes, and containers. And we’ll be using the docker command line interface (CLI) to get these tasks completed quickly.

How To Remove Docker Images

Removing outdated or unnecessary images from Docker is essential for maintaining a clean and organized system. Let’s look at how the CLI can target certain images for removal.

First, let’s look at the images in a Docker environment with the docker image ls command:

$ docker image ls
REPOSITORY       TAG       IMAGE ID       CREATED              SIZE
my_image         latest    2cbc27836df4   60 seconds ago       7.05MB
<none>           <none>    85b412789704   2 days ago           7.05MB
demo             latest    26d80cd96d69   15 months ago        28.5MB

Listing images above has revealed images named my_image and demo, both tagged latest. A third image has no name or tag. In this case, it’s a “dangling” image: one that is not currently used by a container. This dangling image resulted from a rebuilding of my_image (a common scenario). The previous version of the image is still on the file system, but is not now in use.

The listing also displays the first 12 characters of each image’s 64-character ID, creation dates, and the storage space the images are consuming.

Remove All Unused Images

In the listing example above, we have a dangling image. However, it is possible for an image with no name or tag to be active. For example, it could have been used to start a container by referencing its ID.

To safely delete all images that are truly dangling, you can use the docker image prune command:

$ docker image prune

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

Deleted Images:
deleted: sha256:85b412789704c17e9c5e7edc97b2f64c748bbdae7eaa44d3fe2cc21a87acad3d

Total reclaimed space: 7.05MB

We’ll list our images again to see the results of our work:

$ docker image ls
REPOSITORY       TAG       IMAGE ID       CREATED              SIZE
my_image         latest    2cbc27836df4   70 seconds ago       7.05MB
demo             latest    26d80cd96d69   15 months ago        28.5MB

After the pruning process, the image with no name or tag has disappeared.

Remove a Specific Image by Name

You can target an image for removal by referencing its name:

$ docker image rm <image_name>

Note: You can’t easily remove images still in use. You should stop or remove the running containers that are using the images. While it’s not usually recommended, you can force the removal of an active image using the -f flag:

$ docker image rm <image_name> -f

Using the rmi Shorthand For Removing Images

The Docker CLI has a shorthand approach to removing images in the alias rmi. Using it to remove an image by name looks like this:

$ docker rmi demo
Untagged: demo:latest
Deleted: sha256:2cbc27836df4a7707e746e28368f1fdecfefbfb813aae06611ca6913116b80b4

We’ll use rmi for the following image-removal examples.

Remove All Untagged Images

Untagged images still occupy valuable disk space, which slows your system down more and more over time.

To eliminate all untagged images in Docker, use the docker rmi command with a filter. You can provide filters based on specific criteria using the -f option (not to be confused with the -f flag available for image rm to force an action).

The filter dangling=true is used to identify untagged images:

$ docker rmi $(docker images -f "dangling=true" -q)

The -q option in the above command displays the image IDs of all untagged images. All these IDs are then passed as arguments to docker rmi to remove them.

This filter uses the keyword “dangling,” but as noted above, some images without tags might actually be active. You will be warned if an image selected by this filter is not truly dangling.

Remove a Specific Image by ID

You can use the docker rmi command to remove any specific image by referencing its ID:

$ docker rmi <image_id>

Remove a Specific Image by Name and Tag

You can delete a specific image along with its name and tag by using the docker rmi command.

For instance, if you have multiple images with the same name but different tags, you can delete a particular one by using this format:

$ docker rmi <image_name>:<tag>

This method is handy when you want to delete a specific version of an image instead of deleting all images related to a repository.

Remove All Images With the Latest Tag

In Docker, the “latest” tag refers to the most recent version of a particular Docker image. The latest tag is often used as the default tag for Docker images. It is automatically assigned to the most recent build of an image unless another tag is specified explicitly.

Docker provides a command to remove all the images with the “latest” tag:

$ docker rmi $(docker images | grep "latest" | awk '{print $3}')

The above command is divided into 2 parts. First it gets the list of image IDs, and then it passes this list as an argument to the docker rmi command.

Remove Images From a Remote Repository

To remove an image from a remote repository, such as Docker Hub, you’ll first need to log into the account using the Docker CLI.

Once you are logged in, you just need to use the docker rmi command to remove the image. The docker rmi command can remove both local and remote images:

docker rmi my_repo/my_image_tag

Notably, you can only remove images that have been pushed to the repository. Likewise, you cannot remove images used by running containers.

Remove Multiple Images From a Remote Repository

To remove multiple images from a remote repository, you can use the docker rmi command followed by the image IDs or tags. For example:

docker rmi my_repo/image_tag_1 my_repo/image_tag_2 my_repo/image_tag_3

This will remove the three images with the tags image_tag_1, image_tag_2, and image_tag_3 from the repository my_repo.

How To Remove Docker Volumes

Docker volumes can take up a lot of disk space, especially if they contain large amounts of data or multiple backups. By removing volumes that are no longer needed, you reduce the risk of data breaches and ensure sensitive data isn’t accessible to unauthorized users. Regularly removing volumes can also keep the Docker environment up-to-date and prevent issues caused by outdated data.

These commands permanently destroy all the data stored in a volume, so use them with caution.

Remove All Unused Volumes

In Docker, removing unused volumes is as critical as removing images or containers.

To free up disk storage, you can use the docker volume prune command.

Remove a Specific Volume by Name

Removing a specific volume in Docker is useful for removing unused volumes. Here’s the command to remove a volume:

docker volume rm my_volume_name

With the above command, the volume my_volume_name will be removed. Docker produces an error if you try to remove a used volume. You can check available volumes using the docker volume ls command.

How To Remove Docker Containers

As you work more with Docker, you may accumulate many unused containers which occupy the disk resources. If you don’t delete them, they can accumulate over time and take up valuable disk resources, slowing down your system and potentially putting you at risk.

Remove All Stopped Containers

The command docker container prune can delete all stopped containers in Docker. Here, the command creates a list of all stopped containers that will be removed and asks for confirmation before proceeding. This helps reclaim disk space and maintain a clean and organized Docker environment while allowing you to double-check you’re not losing anything important:

$ docker container prune

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Deleted Containers:
4df4c47c4df4
d35bcec20bce

Note that only stopped containers can be removed with this command. If you need to remove running containers, you’ll need to stop them first or use a different command.

Remove a Specific Container by ID

In Docker, you can remove a specific container by ID using the docker rm command along with the container ID. First, get all the container ID using the docker ps command:

$ docker ps -a -q
1ce3cdeb4035
06b79541e25c
fa98f1804e3e

$ docker rm 1ce3cdeb4035

The above command works only if the container is in a stopped state. If the container is running, then you need to forcefully remove it using the -f flag:

$ docker rm -f 1ce3cdeb4035

Remove a Specific Container by Name

Docker provides a multiple-purpose docker rm command to remove both containers by name and by ID.

For example, if you have a container named web_server, you can remove it using the following command:

$ docker rm web_server

First, it is important to stop the container. Docker provides the docker stop command to stop a specific container.

Remove All Running Containers

To delete all the running containers, you can use the docker rm command:

$ docker rm $(docker ps -q)

Using docker ps -q lists the IDs of running containers. After that, IDs are passed to the docker rm command, which removes all of the containers.

Note that this command will only remove running containers. If you want to remove all containers, including stopped ones, you can use the following command instead:

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

Summary

This article explained how to remove Docker images, volumes, and containers. You learned various methods to maintain a clean and organized system. It covered commands to remove all unused, untagged, or specific images by ID or name. You also learned commands to delete all stopped containers or a specific container by ID or name.

Removing Docker images, volumes, and containers is a straightforward process that can help you manage unused components, recovering valuable disk space and often improving system performance.

It’s this simplicity that attracts developers to Docker, and there’s no simpler way to deploy an application developed in Docker than with Kinsta’s Application Hosting platform. You can try it for free now.

Salman Ravoof

Salman Ravoof is a self-taught web developer, writer, creator, and a huge admirer of Free and Open Source Software (FOSS). Besides tech, he's excited by science, philosophy, photography, arts, cats, and food. Learn more about him on his website, and connect with Salman on Twitter.