How to remove docker containers, images, volumes and networks

Docker allows you to quickly build, test, and deploy applications as portable self-contained containers that can run almost anywhere.

Docker does not remove unused objects like containers, images, volumes and networks unless you explicitly tell it to. When working with Docker, you can easily accumulate a large number of unused objects that consume significant disk space and clutter up the output generated by Docker commands.

This guide serves as a cheat sheet to help Docker users organize their system and free up disk space by removing unused Docker containers, images, volumes, and networks.

Team docker system prune will remove all stopped containers, all dangling images and all unused networks:

docker system prune

You will be prompted to continue, use the flag -f or --force to bypass the request.

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]

If you also want to delete all unused volumes, pass the flag --volumes:

docker system prune --volumes
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

Docker containers are not automatically deleted when they stop, unless you start the container using a flag --rm

To remove one or more Docker images use the command docker container rmfollowed by the identifier of the containers you want to delete.

You can get a list of all active and inactive containers by passing the flag -a team docker container ls:

docker container ls -a

The result should look something like this:

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

Once you know about containers CONTAINER IDyou want to delete go to the command docker container rm… For example, to remove the first two containers listed in the above release, run:

docker container rm cc3f2ff51cab cd20b396a061

If you get an error similar to the one below, it means the container is running. Before removing a container, you must stop the container.

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

Before executing the delete command, you can get a list of all non-working (stopped) containers that will be removed with the following command:

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

To remove all stopped containers use the command docker container prune:

docker container prune

You will be prompted to continue, use the flag -f or --force to bypass the request.

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

Team docker container prune allows you to remove containers based on a condition using a filter flag --filter… At the time of this writing, the currently supported filters are until and label… You can use multiple filters by passing multiple flags --filter

For example, to delete all images taken more than 12 hours ago, run:

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

You can get a list of all Docker containers on your system using the command docker container ls -aq

To stop all running containers use the command docker container stopfollowed by a list of all container IDs.

docker container stop $(docker container ls -aq)

After stopping all containers, you can remove them using the command docker container stopfollowed by a list of container IDs.

docker container rm $(docker container ls -aq)

To remove one or more Docker images, use the command docker images lsto find the id of the images you want to remove.

docker image ls

The result should look something like this:

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 located the images you want to delete, transfer them IMAGE ID to the team docker image rm… For example, to remove the first two images listed in the above output, do:

docker image rm 75835a67d134 2a4cca5ac898

If you get an error similar to the one below, it means the image is being used by an existing container. To delete an image, you need to delete the container first.

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

Docker provides the command docker image prunewhich can be used to remove dangling and unused images.

A torn image is an image that is not tagged or used by any container. To remove dangling images, enter:

docker image prune

You will be prompted to continue, use the flag -f or --force to bypass the request.

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

When deleting torn images, if the images you created are not flagged, they will also be deleted.

To remove all images that are not referenced by any existing container, not just dangling ones, use the flag -a:

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

Through commands docker image prune you can remove images based on condition using filter flag --filter… At the time of this writing, the currently supported filters are untiland label… You can use multiple filters by passing multiple flags --filter

For example, to delete all images taken more than 12 hours ago, run:

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

To remove one or more Docker tones, use the command docker volume lsto find the ID of the volumes you want to delete.

docker volume ls

The result should look something like this:

DRIVER              VOLUME NAME
local               4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local               terano

When you find the volumes VOLUME NAMEyou want to delete, pass them to the command docker volume rm… For example, to delete the first volume listed in the above output:

docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

If you receive an error similar to the following, it means that this volume is being used by an existing container. To remove a volume, you need to remove the container first.

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

To remove all unused volumes, use the command docker image prune:

docker volume prune

You will be prompted to continue, use the flag -f or --force to bypass the request.

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

To remove one or more Docker networks, use the command docker network lsto find the ID of the networks you want to remove.

docker network ls

The result should look something like this:

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

Once you have located the networks you want to remove, transfer them NETWORK ID to the team docker network rm… For example, to delete a network named my-bridge-network run:

docker network rm c520032c3d31

If you get an error similar to the following, it means the network is being used by an existing container. To remove a network, you first need to remove the container.

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

Use the command docker network prune to remove all unused networks.

docker network prune

You will be prompted to continue, use the flag -f or --force to bypass the request.

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

Using the command docker network prune you can remove networks based on condition using filter flag --filter… At the time of this writing, the currently supported filters are until and label… You can use multiple filters by passing multiple flags --filter

For example, to remove all networks created more than 12 hours ago, run:

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

In this tutorial, we have shown you some of the common commands for deleting Docker containers, images, volumes and networks.

If you have any questions, please leave a comment below.

Related Posts