Docker is an open source containerized platform that lets you quickly build, test, and deploy applications, and is a portable container that can run anywhere.

With Docker, you quickly accumulate a large number of unused objects, which can take up a lot of disk space and mess up the output produced by Docker commands. Docker does not remove unused objects such as containers, images, data volumes, and networks unless explicitly instructed to do so.

This guide is a cheat sheet to help Docker users keep their systems in order and free up disk space by removing unused Docker containers, images, data volumes, and networks.

Delete all unused Docker objects

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

docker system prune
Copy the code

This command will prompt you to confirm the action:

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]
Copy the code

Use the -for –force options to bypass the prompt. By default, this command does not delete unused volumes to prevent the loss of important data. To delete all unused volumes, pass the — Volumes option:

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
Copy the code

Delete the Docker container

Docker containers are not automatically deleted when they are stopped, unless they are started with the –rm flag.

Delete one or more containers

To delete one or more Docker containers, use the Docker container rm command followed by the ID of the container to be deleted.

You can get a list of all containers by calling the command with the following option: Docker Container ls -a

docker container ls -a
Copy the code

The output should look 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... "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_mirzakhaniCopy the code

After knowing the CONTAINER ID of the CONTAINER to be deleted, pass it to the docker CONTAINER rm command. For example, to delete the first two containers listed in the output above, run:

docker container rm cc3f2ff51cab cd20b396a061
Copy the code

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

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

Delete all stopped containers

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

docker container prune

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

To avoid prompts, use the -for –force option.

To get a list of all the non-running (stopped) containers that will pass the delete docker Container Prune, use the following command:

docker container ls -a --filter status=exited --filter status=created 
Copy the code

Use a strainer to remove the container

The Docker Container Prune command allows you to delete containers based on specific criteria using the –filter option. At the time of this writing, the filters currently supported are until and label. You can specify multiple filters using multiple –filter options. For example, to delete all images created 12 hours ago, run:

docker container prune --filter "until=12h"
Copy the code

Stop and remove all containers

To stop all running containers, use the docker container stop command followed by the container ID:

docker container stop $(docker container ls -aq)
Copy the code

The docker container ls -aq command generates a list of all containers. Once all containers are stopped, remove them using the docker container rm command, followed by the list of container ids.

docker container rm $(docker container ls -aq)
Copy the code

Delete a Docker image

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

Delete one or more mirrors

To delete one or more Docker images, you first need to find the ID of the image:

docker image ls
Copy the code

The output should look 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 311MBCopy the code

After finding the IMAGE to be deleted, pass its IMAGE ID to the Docker IMAGE rm command. For example, to delete the first two images listed in the output above, run:

docker image rm 75835a67d134 2a4cca5ac898
Copy the code

If you receive the following error message, the image is being used by an existing container. To delete an image, you must first delete the container.

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

Delete the dangling image

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

An unmarked image that is not used by any container. To remove the dangling image, type:

docker image prune

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

Use the -for –force options to bypass the prompt.

Be careful when running this command. If the image you build has no labels, delete it

Delete all unused mirrors

To remove unreferenced images from all existing containers, not just the container for dangling, use Prune’s command with the following -a option:

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
Copy the code

Delete an image using a filter

With the Docker Image Prune command, you can also remove images based on specific criteria using the –filter option.

At the time of this writing, the filters currently supported are until and label. You can use more than one filter.

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

docker image prune -a --filter "until=12h"
Copy the code

Delete the Docker data volume

Delete one or more volumes

To delete one or more Docker volumes, run the Docker volume ls command to find the ID of the volume you want to delete.

docker volume ls
Copy the code

The output should look like this:

DRIVER              VOLUME NAME
local               4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local               terano
Copy the code

After finding the volumes VOLUME NAME wants to delete, pass them to the Docker VOLUME rm command. For example, to remove the first volume listed in the output above, run:

docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
Copy the code

If you receive an error similar to the one shown below, the volume is being used by an existing container. To delete the volume, you must first delete the container.

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

Delete all unused volumes

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

docker volume prune

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

Use the -for –force options to bypass the prompt.

Delete the Docker network

Delete one or more networks

To delete one or more Docker networks, use the Docker network ls command to find the ID of the network to delete.

docker network ls
Copy the code

The output should look 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
Copy the code

After finding the NETWORK to be deleted, pass its NETWORK ID to the Docker NETWORK rm command. For example, delete the my-bridge-networkRun network:

docker network rm c520032c3d31
Copy the code

If you receive an error similar to the one shown below, the network is being used by an existing container. To delete a network, you must first delete the container.

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

Delete all unused networks

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

docker network prune
Copy the code

You will be prompted to continue:

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

Use the -for –force options to bypass the prompt.

Delete the network using a filter

Using the Docker network prune command, you can delete networks based on criteria using the –filter option.

At the time of this writing, the filters currently supported are until and label. You can use multiple –filter options to use multiple filters. For example, to delete all networks created 12 hours ago, run:

docker network prune -a --filter "until=12h"
Copy the code

conclusion

In this guide, you are shown some common commands for deleting Docker containers, images, data volumes, and networks. If you have any questions, please comment below.