How to Remove Docker Images, Containers, Volumes, and Networks

By 

Updated on

9 min read

Remove Docker Containers, Images, Volumes, and Networks

Docker is an open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run virtually anywhere.

When working with Docker, you can quickly accumulate a large number of unused objects that consume significant disk space and clutter the output produced by Docker commands. Docker does not remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so.

This article explains how to remove Docker containers, images, volumes, and networks, both selectively and in bulk.

Quick Reference

For a printable quick reference, see the Docker cheatsheet .

TaskCommand
Remove all unused objectsdocker system prune
Remove all unused objects and imagesdocker system prune -a
Remove unused objects and anonymous volumesdocker system prune --volumes
Remove a containerdocker container rm CONTAINER_ID
Force remove a running containerdocker container rm -f CONTAINER_ID
Remove all stopped containersdocker container prune
Stop all running containersdocker container stop $(docker container ls -q)
Remove all containersdocker container rm $(docker container ls -aq)
Remove an imagedocker image rm IMAGE_ID
Remove all dangling imagesdocker image prune
Remove all unused imagesdocker image prune -a
Remove a volumedocker volume rm VOLUME_NAME
Remove unused volumesdocker volume prune
Remove all unused volumesdocker volume prune -a
Remove a networkdocker network rm NETWORK_ID
Disconnect a container from a networkdocker network disconnect -f NETWORK CONTAINER
Remove all unused networksdocker network prune

Removing All Unused Docker Objects

The docker system prune command removes all stopped containers, dangling images, and unused networks:

Terminal
docker system prune

You will be prompted to confirm the operation:

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]

Use the -f (--force) option to bypass the prompt.

If you want to remove all unused images, not just the dangling ones, add the -a (--all) option to the command:

Terminal
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]

By default, the command does not remove volumes to prevent important data from being deleted. To include unused anonymous volumes in the cleanup, pass the --volumes option:

Terminal
docker system prune --volumes
output
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all anonymous 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 you stop them unless you start the container using the --rm flag.

Removing one or more containers

To remove one or more Docker containers, use the docker container rm command, followed by the IDs of the containers you want to remove.

You can get a list of all containers by invoking the docker container ls command with the -a option:

Terminal
docker container ls -a

The output will look something like this:

output
CONTAINER ID   IMAGE      COMMAND       CREATED        STATUS                      NAMES
cc3f2ff51cab   ubuntu     "/bin/bash"   2 months ago   Created                     competent_nightingale
cd20b396a061   nginx      "/bin/bash"   2 months ago   Exited (137) 2 months ago   web
fb62432cf3c1   postgres   "/bin/bash"   3 months ago   Exited (130) 3 months ago   db

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

Terminal
docker container rm cc3f2ff51cab cd20b396a061

If you get an error similar to the one shown below, it means that the container is running. You need to stop the container before removing it.

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

To force the removal of a running container, use the -f (--force) option:

Terminal
docker container rm -f fc983ebf4771

Removing all stopped containers

To remove all stopped containers, invoke the docker container prune command:

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

If you want to get a list of all non-running (stopped) containers that would be removed with docker container prune, use the following command:

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

Removing containers using filters

The docker container prune command allows you to remove containers based on a certain condition using the --filter option.

The currently supported filters are until and label. You can specify more than one filter by using multiple --filter options.

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

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

Stop and remove all containers

To stop all running containers, enter the docker container stop command followed by the running container IDs:

Terminal
docker container stop $(docker container ls -q)

The command docker container ls -q generates a list of running container IDs.

Once all containers are stopped, remove them using the docker container rm command, followed by the container ID list:

Terminal
docker container rm $(docker container ls -aq)

Removing Docker Images

When you download a Docker image, it is kept on the server until you manually remove it.

Removing one or more images

To remove one or more Docker images, first find the IDs of the images you want to delete:

Terminal
docker image ls

The output will look something like this:

output
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
ubuntu       latest    2a4cca5ac898   2 months ago   78MB
nginx        latest    a45d6dca3361   3 months ago   192MB
postgres     16        e44d62cf8862   3 months ago   432MB

Once you have located the images you want to remove, pass their IMAGE ID to the docker image rm command. For example, to remove the first two images listed in the output above, run:

Terminal
docker image rm 2a4cca5ac898 a45d6dca3361

You can also use the shorter docker rmi alias:

Terminal
docker rmi 2a4cca5ac898 a45d6dca3361

If you get an error like the one below, it means that an existing container uses the image. To remove the image, you will have to remove the container first.

output
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container cd20b396a061 is using its referenced image 2a4cca5ac898

Removing dangling images

Docker provides a docker image prune command that can be used to remove dangling and unused images.

A dangling image is an image that is not tagged and is not used by any container. To remove dangling images, type:

Terminal
docker image prune
output
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Warning
Be careful when running this command. If you built an image without a tag, it would be removed.

Removing all unused images

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

Terminal
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 docker image prune command, you can also remove images based on a particular condition with the --filter option.

The currently supported filters are until and label. You can use more than one filter.

For example, to remove all images that were created more than seven days (168 hours) ago, run:

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

Removing Docker Volumes

Removing one or more volumes

To remove one or more Docker volumes, run the docker volume ls command to find the name of the volumes you want to remove:

Terminal
docker volume ls

The output will look something like this:

output
DRIVER    VOLUME NAME
local     4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local     terano

Once you have found the VOLUME NAME of the volumes you want to remove, pass them to the docker volume rm command. For example, to remove the first volume listed in the output above, run:

Terminal
docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

If you get an error similar to the one shown below, it means that an existing container uses the volume. To remove the volume, you will have to remove the container first.

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

Removing unused volumes

To remove unused volumes, run the docker volume prune command:

Terminal
docker volume prune
output
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N]

Use the -f or --force option to bypass the prompt.

By default, this removes only anonymous volumes. To include named volumes, add the -a (--all) option:

Terminal
docker volume prune -a

Removing Docker Networks

Removing one or more networks

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

Terminal
docker network ls

The output will look something like 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

Once you have located the networks you want to remove, pass their NETWORK ID to the docker network rm command. For example, to remove the network with the name my-bridge-network, run:

Terminal
docker network rm c520032c3d31

If you get an error similar to the one shown below, it means that a container is still connected to the network.

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

The docker network rm -f command does not force-remove a network that still has active endpoints. The -f option only suppresses the error if the network does not exist.

To remove an in-use network, disconnect the containers from the network first, then remove it. The -f flag on docker network disconnect forces disconnection even while the container is running:

Terminal
docker network disconnect -f my-bridge-network container_name

Once all endpoints are disconnected, run docker network rm again.

Removing all unused networks

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

Terminal
docker network prune

You will be prompted 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

With the docker network prune command, you can remove networks based on a condition using the --filter option.

The currently supported filters are until and label. You can use more than one filter by using multiple --filter options.

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

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

Conclusion

Keeping Docker objects clean prevents disk usage from growing unchecked. The safest starting point is docker system prune, which removes only stopped containers, dangling images, and unused networks. Add --volumes or -a when you need a deeper cleanup.

For related Docker management tasks, see how to list containers , build images with Dockerfile , or run containers .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page