Docker Tips: How to Delete All Docker Containers and Images

Learn how to delete all Docker containers and images with simple commands. This guide explains safe practices, automation options, and key differences between docker rm and docker rmi.

Docker Tips: How to Delete All Docker Containers and Images

It's time to start sharing some tips I've found while using Docker. You may be required to clean up your Docker installation at some point.

If you have used Docker for any length of time, you will start to notice unused containers and images lying around. Sometimes, this can become a burden on your disk space or just an administrative hassle. Either way, it is a quick and easy fix.

Before proceeding, please be aware that these commands will nuke every non-running container and image not associated with a running container. You've been warned.

Delete all Docker containers that are not running

root@Docker01:~# docker rm $(docker ps -a -q)

Delete all Docker Images

I've provided two different options for deleting images. To correctly understand the difference between Tagged and UnTagged Images, please take a look at the diagram below.

Docker Image Tag Diagram

Source Docker.com

Option 1 - Delete all untagged Docker Images.

, which also works quite well. I tested it, and itThis was updated based on Matthew Torres's comment below as it is much easier to delete with the filtering flag Check out Docker's Filtering Docs for more information.

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

Option 2 - Delete all Docker Images

docker rmi $(docker images -q)

Updated Options

Jan responded on Twitter with the following proposal, which also works quite well. I tested it, and it works well.

@docker @idomyowntricks use formatting instead of trying to remove all incl. running ones (-qa): docker rm $(docker ps -qf status=exited)— Jan (@KekSfabrik_EU) February 23, 2016

Option 3 - Delete all Docker Containers according to formatting

docker rm $(docker ps -qf status=exited)

FAQ Section: How to Delete All Docker Containers and Images

How can I delete all Docker containers at once?
To delete all Docker containers at once, you can use the command docker rm $(docker ps -a -q). This command first lists all container IDs (docker ps -a -q) and then removes them (docker rm). For containers that are currently running, you must stop them first with docker stop $(docker ps -q) before running the removal command.

What is the command to delete all Docker images?
To delete all Docker images, you can use docker rmi $(docker images -q). This command works similarly to deleting containers: it lists all image IDs (docker images -q) and then removes them (docker rmi). If some images are in use by containers, you need to delete those containers first.

Is it safe to delete all Docker containers and images?
Deleting all Docker containers and images is generally safe if you no longer need them. However, this action is irreversible, so be sure to back up any important data or configurations before proceeding. Once deleted, you'll need to rebuild or pull the images again if you want to use them in the future.

Can I automate the process of cleaning up Docker containers and images?
Yes, you can automate the cleanup process by creating a shell script that runs the necessary Docker commands. Additionally, Docker provides the docker system prune command, which can be used to clean up unused containers, images, networks, and volumes. Running docker system prune -a will remove all unused images and stopped containers, making it a more comprehensive cleanup option.

What is the difference between docker rm and docker rmi?
docker rm is used to delete Docker containers, while docker rmi is used to delete Docker images. Containers are instances of images running as applications, and images are the packaged environments and code used to create containers. It's important to understand the distinction to avoid accidentally deleting important images when you only intend to remove containers.

Follow me

If you liked this article, Follow Me on Twitter to stay updated!