How to Remove Docker Containers, Images and Volumes

Introduction

Before we discuss how to remove docker container, images and volumes, let's first understand-What is Docker ?

Docker is an open-source containerization platform that allows you to develop, test, and deploy programs as portable containers that can operate almost anywhere.

When using Docker, it is easy to quickly gather a lot of unused objects that take up a lot of disk space and clog up the output of the Docker commands. Docker will not remove unused objects like volumes, networks, images, or containers unless you explicitly instruct it to.

This tutorial acts as a “cheat sheet” to assist Docker users in maintaining system organization and freeing up disk space by removing unused Docker containers, images, volumes, and networks.

Removing All Unused Docker Objects

All stopped containers, dangling images, and unused networks are removed using the docker system prune command:

docker system prune

You will be asked to confirm the action:

Output

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

Bypass the prompt by using the-f (--force) option.

Add the -a (--all) option to the command if you want to remove all unused images, rather than just the ones that are dangling:

docker system prune -a
Output

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all images without at least one container associated to them
        - all build cache
Are you sure you want to continue? [y/N]

To avoid losing crucial data, the command does not remove unused volumes by default. To remove all unused volumes, use the--volumesoption:

docker system prune --volumes
Output

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Removing Docker Containers

Docker containers are not automatically removed when they are stopped, unless they are started using the --rm flag.

Removing one or more containers

To remove one or more Docker containers, use the docker container rm command, accompanied by the IDs of the containers to be removed.

By using the docker container ls command with the -a option, you can obtain a list of all containers:

docker container ls -a

The output should resemble this:

Output

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
cc3f2ff51cab        centos                  "/bin/bash"              2 months ago        Created                                         competent_nightingale
cd20b396a061        solita/ubuntu-systemd   "/bin/bash -c 'exec …"   2 months ago        Exited (137) 2 months ago                       systemd
fb62432cf3c1        ubuntu                  "/bin/bash"              3 months ago        Exited (130) 3 months ago                       jolly_mirzakhani

After you know the CONTAINER ID of the containers you want to remove, pass it to the docker container rm command. For instance, you would run the following command to remove the first two containers listed in the output above:

docker container rm cc3f2ff51cab cd20b396a061

The container is running if you receive an error message such as the one shown below. You must stop the container prior to removing it.

Output

Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.

Removing all stopped containers

You need to use the docker container prune command to remove all stopped containers:

docker container prune
Output

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

Use the following command to obtain a list of all stopped (non-running) containers that will be removed by docker container prune:

docker container ls -a --filter status=exited --filter status=created

Removing containers using filters

Using the --filter option of the docker container prune command, you can remove containers based on a specific criterion.

The filters that are currently supported are until and label as of the time this article was being written. You can provide multiple filters by using multiple --filter options.

For example, to remove any images created more than 12 hours ago, run:

docker container prune --filter "until=12h"

Stop and remove all containers

Enter the docker container stop command followed by the container IDs to stop all running containers:

docker container stop $(docker container ls -aq)

All containers are listed by using the docker container ls -aq command.

Once all containers have been stopped, remove them by using the docker container rm command and the containers ID list.

docker container rm $(docker container ls -aq)

Removing Docker Images

A Docker image that you download is stored on the server until you manually remove it.

Removing one or more images

To delete one or more Docker images, you must first locate their IDs:

docker image ls

The output would appear as follows:

Output

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos                  latest              75835a67d134        7 days ago          200MB
ubuntu                  latest              2a4cca5ac898        2 months ago        111MB
linuxize/fedora         latest              a45d6dca3361        3 months ago        311MB
java                    8-jre               e44d62cf8862        3 months ago        311MB

Once you have found the images you wish to remove, provide the docker image rm command their IMAGE ID. For instance, you would execute the following command to remove the first two images listed in the output above:

docker image rm 75835a67d134 2a4cca5ac898

If you receive an error message similar to the one below, it indicates that the image is being used by another container. You must first remove the container in order to remove the image.

Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container cd20b396a061 is using its referenced image 75835a67d134

Removing dangling images

Docker includes a docker image prune command for removing dangling and unused images.

A dangling picture is one that has not been tagged and is not being used by any container. To remove dangling images, enter:

docker image prune
Output

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

💡
Be cautious when running this command. An image would be removed if it was built without a tag.

Removing all unused images

Use the prune command with the -a option to remove all images, not just the dangling ones, that are not referenced by any existing containers:

docker image prune -a
Output

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

Removing images using filters

With the --filter option of the docker image prune command, you can also remove pictures based on a specific criterion.

The filters that are currently supported are until and label as of the time this article was being written. You can use multiple filters.

For instance, you would run the following command to remove any images created more than seven days (168 hours) ago:

docker image prune -a --filter "until=168h"

Removing Docker Volumes

Removing one or more volumes

To remove one or more Docker volumes, use the docker volume ls command to locate the ID of the volumes you want to remove.

docker volume ls

The output should resemble this:

Output

DRIVER              VOLUME NAME
local               4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local               terano

Pass the volumes you want to remove to the docker volume rm command once you have identified their VOLUME NAME. For instance, execute the following command to remove the first volume shown in the output above:

docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

The volume is being used by an existing container if you encounter an error like the one below. You must first remove the container in order to remove the volume.

Error response from daemon: remove 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e]

Removing all unused volumes

Run the docker image prune command to remove all unused volumes:

docker volume prune
Output

WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]

To bypass the prompt, use the -f or --force options.

Removing Docker Networks

Removing one or more networks

Using the docker network ls command, locate the ID of the networks you want to remove from one or more Docker networks.

docker network ls

The output should resemble this:

Output

NETWORK ID          NAME                DRIVER              SCOPE
107b8ac977e3        bridge              bridge              local
ab998267377d        host                host                local
c520032c3d31        my-bridge-network   bridge              local
9bc81b63f740        none                null                local

When you have found the networks you want to remove, provide the docker network rm command their NETWORK ID. Run these commands, for instance, to remove the network called my-bridge-network:

docker network rm c520032c3d31

An existing container is using the network if you encounter an error like the one below. You must first remove the container in order to remove the network.

Error response from daemon: network my-bridge-network id 6f5293268bb91ad2498b38b0bca970083af87237784017be24ea208d2233c5aa has active endpoints

Removing all unused network

To remove all unused networks, use the docker network prune command.

docker network prune

You will be asked to continue:

Output

WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] 

Removing networks using filters

Using the--filter option of the docker network prune command, you can remove networks depending on a criterion.

The filters that are currently supported are until and label as of the time this article was being written. By using multiple --filter options, you can apply multiple filters.

For instance, you would run the following command to remove all networks that are created more than 12 hours ago:

docker network prune -a --filter "until=12h"

FAQs to Remove Docker Containers, Images and Volumes

How do I remove all stopped Docker containers?

To remove all stopped containers, execute docker container prune command.

Can I remove all unused volumes at once?

Yes, you can remove all unused volumes simultaneously by running docker volume prune.

How can I remove all unused networks?

Execute docker network prune to remove all unused Docker networks.

Is there a way to force removal of containers or images?

Yes, you can force removal by adding the -f flag to the respective command (e.g., docker rm -f <container_name>).

Can I remove multiple containers/images/volumes/networks at once?

Yes, you can remove multiple containers, images, volumes, or networks by specifying their names or IDs separated by spaces. Use commands like docker rm <container1> <container2> or docker rmi <image1> <image2>.

Is there a command to remove both containers and their associated volumes and networks? 

Yes, the docker system prune command can be used to remove all unused containers, volumes, networks, and images.

Can I undo the removal of Docker resources?

No, the removal of Docker resources is irreversible. Make sure to backup any important data before deleting them.

Conclusion

In this tutorial, we have covered some common commands for removing Docker containers, images, volumes, and networks.

Check out the official Docker documentation for more information.

If you have any queries, feel free to leave a comment below, and we'll be happy to help.