How to Delete Docker Images, Containers, and Volumes
Docker is one of the most sought-after tools in development and DevOps. However, sometimes, there are a lot of images, containers, and volumes that are no longer needed. Their overabundance can lead to application slowdowns and disk clutter, so you need to get rid of this "garbage" occasionally.
Docker has enough features to clean the system periodically, and you can do it directly from the command line. Once you memorize the commands below, the cleaning process will not be much more complicated than using the "Recycle Bin" in the OS.
Deleting all objects Copy link
First, here is a command that removes everything, including so-called "dangling" objects:
docker system pruneTo delete stopped objects, you will need the -a flag, like this:
docker system prune -aImages that are no longer used are destroyed like this:
docker image prune -aThis command also includes child images and metadata.
Now get rid of all containers:
docker container pruneAnd finally, get rid of the volumes:
docker volume pruneTo ensure you are removing the correct objects, use the --dry-run or -n flags to view the objects without destroying them.
In addition, these commands delete objects without the ability to restore them. Therefore, before following the instructions above (and below), make sure that you understand what objects will be deleted and that you are not deleting something important.
How to delete Docker images Copy link
Here, we will look at basic instructions for deleting a certain number of images with or without specified parameters.
Deleting specific images Copy link
To do this, first type:
docker images -aThe command allows you to display a list of all Docker images on the local machine, including those not currently in use. The -a flag tells Docker to display all available images, not just recently created.
Next, enter:
docker rmi Img ImgInstead of Img, substitute the image names to be deleted. Multiple names should be separated by a space.
Deleting "dangling" images Copy link
To get a list of unused images (such objects are also called "dangling"), type:
docker images -f dangling=trueSuch images can occur when creating new images based on old ones with the same name but without the tag. In this case, the old image becomes dangling and is not used. The -f option filters objects by different criteria; in this case, we filter by the dangling=true setting.
Now, to delete unused images, enter:
docker image pruneThis command affects dangling images and those that are no longer in use. When it is executed, it will delete the images with no possibility of recovery.
Deleting by pattern Copy link
To search for images that contain some pattern in their names, type:
docker images -a | grep "pattern"-a displays all images, including inactive and intermediate images. grep filters the output to display only those images whose names contain the specified pattern (type it instead of "pattern", keeping the quotation marks).
Now, to remove them, type:
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmiawk '{print $3}' only displays the third output column containing the identifiers. xargs docker rmi is needed to pass identifiers to docker rmi, which removes the given images. xargs breaks the list of image identifiers into separate arguments. If you try to delete multiple images by listing their identifiers manually, you may run into a problem if the identifiers contain spaces or other special characters. In this case, using xargs is safer and more convenient.
To delete images in use, type:
docker rm -f $(docker ps -a -q)This will stop and destroy the containers before deleting the images.
Delete all images Copy link
To display a list of all images on the local machine, including those not currently in use, type:
docker images -aNext, to delete all inactive images on the local machine, type:
docker rmi $(docker images -a -q)This command outputs a list of IDs of all images on the local machine and passes it as an argument to docker rmi, which deletes all Docker images without the possibility of recovery. To delete the active ones used by containers as well, enter:
docker rm -f $(docker ps -a -q)How to delete Docker containers Copy link
In this chapter, we will cover all the basic instructions on how to delete a certain number of containers with or without specified parameters.
Deleting specific containers Copy link
To get started, type:
docker ps -aNow enter this command to delete:
docker rm Name_or_IDTo use it correctly, replace Name_or_ID with the appropriate IDs or names (you can enter multiple objects separating them with spaces).
Also, to delete a container, you should stop it first. If the container is running, type:
docker stop Name_or_IDTo destroy all objects of this type on the local machine, enter:
docker rm $(docker ps -a -q)Deleting containers on exit Copy link
To start a new container based on the specified image, enter, replacing image_name:
docker run --rm image_nameThe --rm flag tells Docker to delete it after it stops.
Now, a little clarification. By default, the created container stays on the local machine after stopping. And if you don't want to keep the container, then use the --rm flag. This is very convenient when launching temporary containers to perform some task, for example, running tests, when you don't want to leave "garbage" behind. Flags such as -v for mounting volumes or -e for passing environment variables will help to save data.
Delete all running containers Copy link
First, type:
docker ps -a -f status=exitedThe -f status=exited flag here filters the output.
Now, to delete, type:
docker rm $(docker ps -a -f status=exited -q)Deleting containers by multiple filters Copy link
To output containers with exited and created statuses, i.e. not running or currently running, type:
docker ps -a -f status=exited -f status=createdThe -a and -f flags here tell Docker to display all containers and filter them.
Now, to delete all objects that are in exited or created states, type:
docker rm $(docker ps -a -f status=exited -f status=created -q)Deleting by pattern Copy link
To get a list of containers on the local machine, and then filter the list to display only those objects whose names have a particular pattern in them, enter by replacing "pattern" with the desired pattern, keeping the quotation marks:
docker ps -a | grep "pattern"Now, to delete objects whose names have the string "pattern" in them, type:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rmThis command uses grep to filter the list, awk '{print $1}' to extract the first column (identifier), and then passes the identifiers using xargs. Let's add that docker ps -a | grep "pattern" can output not only names but also other information: status, ID, etc. Therefore, use the commands above to delete objects with the string "pattern" in their specified parameters.
Stopping and deleting all containers Copy link
To get a list of containers on the local machine, including those not currently running, enter the familiar command:
docker ps -aIt will show information about each object: identifier, name, status, utilized resources, etc.
To stop all containers on the local machine, enter:
docker stop $(docker ps -a -q)And now delete them, like this:
docker rm $(docker ps -a -q)How to delete Docker volumes Copy link
In the final chapter, let's look at the basic commands for deleting a certain number of volumes with or without specified parameters.
Deleting specific volumes Copy link
To get a list of volumes on the local machine, type:
docker volume lsThe command outputs a list of available volumes, including information about them (IDs, names).
To delete a specified number of volumes with specified names, type, adding the volume name instead of vlm_name:
docker volume rm vlm_nameThis command takes a varying number of arguments. To delete multiple objects, list their names separated by a space:
docker volume rm my_volume1 my_volume2If volumes are used by containers in a running state, they cannot be deleted unless those containers are stopped or deleted. Attempts to destroy such objects will result in an error output.
Deleting dangling volumes Copy link
To get a list of dangling volumes on the local machine, type:
docker volume ls -f dangling=trueThe command outputs a list of them, including their IDs and names. You can now get rid of them by typing:
docker volume pruneAll objects of this type that are not associated with any container and are not currently in use will be deleted.
Deleting a container and its volumes Copy link
To delete volumes that were created automatically, type:
docker rm -v container_nameThe -v flag here tells Docker to delete all linked volumes. The point is that such volumes persist on the local machine unless deleted explicitly. Therefore, such an operation can be useful to free up the space occupied by unused volumes on the local machine.
Conclusion Copy link
In conclusion, before you perform the commands for deleting images, containers, and volumes, you should make sure that you or your team members will no longer need the objects you are deleting. After all, all of these operations are irreversible. That's all for now; we wish you success with Docker!
By the way, with Hostman, you can run your workloads on efficient VPS USA or Netherlands VPS that support low latency for EU-based users. For learning, demos, or small services, an inexpensive VPS server is often more than sufficient.