{"id":3574,"date":"2023-11-17T17:12:11","date_gmt":"2023-11-17T09:12:11","guid":{"rendered":"https:\/\/www.servergigabit.com\/guide\/?post_type=kb&#038;p=3574"},"modified":"2026-03-17T11:14:55","modified_gmt":"2026-03-17T03:14:55","slug":"removing-unused-docker","status":"publish","type":"kb","link":"https:\/\/www.servergigabit.com\/guide\/kb\/removing-unused-docker","title":{"rendered":"Removing Unused Docker Images, Containers, and Volumes: A Complete Guide"},"content":{"rendered":"<header class=\"entry-header \">\n<div class=\"entry-meta\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-3575\" src=\"https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/images-9.jpeg\" alt=\"Removing Unused \" width=\"687\" height=\"385\" \/><\/div>\n<\/header>\n<div class=\"entry-content clear\">\n<p><a href=\"https:\/\/www.docker.com\/\" rel=\"nofollow noopener\" target=\"_blank\">Docker<\/a> is a popular tool for managing containerized applications. However, over time, unused Docker images, volumes, and containers can accumulate on your system, consuming valuable disk space. This guide provides a comprehensive overview of the various commands and techniques for removing unused Docker elements, ensuring an efficient and organized Docker environment.<\/p>\n<h2 id=\"h-removing-unused-images-containers-volumes-and-networks\" class=\"wp-block-heading\"><strong>Removing unused Images, Containers, Volumes and Networks<\/strong><\/h2>\n<p data-sourcepos=\"9:1-9:201\">The <code class=\"\">docker system prune<\/code> command effectively removes dangling and untagged resources, including images, containers, volumes, and networks. For a more comprehensive cleanup, including unused images and stopped containers, use the <code class=\"\">-a<\/code> flag:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker system prune -a<\/code><\/pre>\n<\/div>\n<h2 id=\"h-removing-volumes\" class=\"wp-block-heading\"><strong>Removing Volumes<\/strong><\/h2>\n<h3 id=\"h-removing-specific-docker-volumes-nbsp\" class=\"wp-block-heading\"><strong>Removing <em>Specific<\/em> Docker Volumes<\/strong><\/h3>\n<p data-sourcepos=\"17:1-17:189\">o remove specific Docker volumes, use the <code class=\"\">docker volume ls<\/code> command to identify the volume names. Subsequently, employ the <code class=\"\">docker volume rm<\/code> command, specifying the desired volume names:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker volume ls\r\ndocker volume rm [volume_name]<\/code><\/pre>\n<\/div>\n<h3 id=\"h-removing-dangling-docker-volumes\" class=\"wp-block-heading\"><strong>Removing <em>Dangling<\/em> Docker Volumes<\/strong><\/h3>\n<p data-sourcepos=\"24:1-24:210\">For dangling volumes, which persist even after the associated container is deleted, use the <code class=\"\">docker volume ls<\/code> command with the <code class=\"\">-f dangling=true<\/code> filter to identify them. Then, execute the <code class=\"\">docker volume prune<\/code> command to eliminate all dangling volumes:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker volume ls -f dangling=<span class=\"hljs-literal\">true<\/span>\r\ndocker volume prune<\/code><\/pre>\n<\/div>\n<h3 id=\"h-removing-containers-and-associated-volumes-nbsp\" class=\"wp-block-heading\"><strong>Removing <em>Containers and Associated<\/em> Volumes\u00a0<\/strong><\/h3>\n<p data-sourcepos=\"33:1-33:119\">To simultaneously remove a container and its associated unnamed volume, use the <code class=\"\">-v<\/code> flag with the <code class=\"\">docker rm<\/code> command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker rm -v [container_name]<\/code><\/pre>\n<\/div>\n<h2 id=\"h-removing-docker-images-nbsp\" class=\"wp-block-heading\"><strong>Removing Docker Images<\/strong><\/h2>\n<h3 id=\"h-removing-specific-docker-images\" class=\"wp-block-heading\"><strong>Removing <em>Specific<\/em> Docker Images<\/strong><\/h3>\n<p data-sourcepos=\"41:1-41:200\">To identify image IDs for deletion, use the <code class=\"\">docker images -a<\/code> command, which displays all images, including intermediate layers. Then, use the <code class=\"\">docker rmi<\/code> command with the desired image IDs or tags:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker images -a\r\ndocker rmi [Image]<\/code><\/pre>\n<\/div>\n<p>Depending on the number of images on your server, the output will look something like this:<\/p>\n<figure class=\"wp-block-image size-full\"><picture><source srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png.webp 855w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-600x144.png.webp 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-768x184.png.webp 768w\" type=\"image\/webp\" sizes=\"(max-width: 855px) 100vw, 855px\" \/><img decoding=\"async\" class=\"wp-image-16961 webpexpress-processed\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png\" sizes=\"(max-width: 855px) 100vw, 855px\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png 855w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-600x144.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-768x184.png 768w\" alt=\"How To Remove Docker Volumes, Images and Containers\" width=\"855\" height=\"205\" \/><\/picture><\/figure>\n<h3 id=\"h-remove-dangling-images\" class=\"wp-block-heading\"><strong>Remove <em>Dangling<\/em> Images<\/strong><\/h3>\n<p data-sourcepos=\"48:1-48:201\">For dangling images, which are detached from any tagged images, use the <code class=\"\">docker images -f dangling=true<\/code> command to identify them. Subsequently, execute the <code class=\"\">docker image prune<\/code> command to remove them:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker images -f dangling=<span class=\"hljs-literal\">true<\/span>\r\ndocker [Image] prune<\/code><\/pre>\n<\/div>\n<p>Here\u2019s an example for the output:<\/p>\n<figure class=\"wp-block-image size-full\"><picture><source srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png.webp 855w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-600x144.png.webp 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-768x184.png.webp 768w\" type=\"image\/webp\" sizes=\"(max-width: 855px) 100vw, 855px\" \/><img decoding=\"async\" class=\"wp-image-16959 webpexpress-processed\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png\" sizes=\"(max-width: 855px) 100vw, 855px\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22.png 855w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-600x144.png 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/04\/image-22-768x184.png 768w\" alt=\"How To Remove Docker Volumes, Images and Containers\" width=\"855\" height=\"205\" \/><\/picture><\/figure>\n<h3 id=\"h-deleting-images-based-on-a-particular-pattern\" class=\"wp-block-heading\"><strong>Remove Images <em>Based on a Particular Pattern<\/em><\/strong><\/h3>\n<p data-sourcepos=\"55:1-55:195\">To remove images based on a specific pattern, combine the <code class=\"\">docker images<\/code> command with <code class=\"\">grep<\/code> to locate matching images. Then, use <code class=\"\">awk<\/code> and <code class=\"\">xargs<\/code> to pass their IDs to the <code class=\"\">docker rmi<\/code> command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker images -a | grep <span class=\"hljs-string\">\"pattern\"<\/span> | awk <span class=\"hljs-string\">'{print $3}'<\/span> | xargs docker rmi<\/code><\/pre>\n<\/div>\n<h3 id=\"h-remove-all-images-nbsp\" class=\"wp-block-heading\"><strong>Remove <em>All<\/em> Images\u00a0<\/strong><\/h3>\n<p data-sourcepos=\"61:1-61:185\">Finally, to remove all images, use the <code class=\"\">docker images -a<\/code> command to display all Docker images on your system. Then, add the <code class=\"\">-q<\/code> flag to pass the image IDs to the <code class=\"\">docker rmi<\/code> command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker rmi $(docker images -a -q)<\/code><\/pre>\n<\/div>\n<h2 id=\"h-removing-containers-nbsp\" class=\"wp-block-heading\"><strong>Removing Containers<\/strong><\/h2>\n<h3 id=\"h-removing-specific-containers-nbsp\" class=\"wp-block-heading\"><strong>Removing S<em>pecific<\/em> Containers\u00a0<\/strong><\/h3>\n<p data-sourcepos=\"69:1-69:209\">To identify containers for removal, use the <code class=\"\">docker ps -a<\/code> command, which displays all containers, including those that are stopped or exited. Then, use the <code class=\"\">docker rm<\/code> command with the container IDs or names:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker ps -a\r\ndocker rm [CONTAINER-ID-OR-NAME]<\/code><\/pre>\n<\/div>\n<h3 id=\"h-delete-a-container-automatically-when-it-exits-nbsp\" class=\"wp-block-heading\"><strong>Remove a Container <em>Automatically<\/em> When It Exits\u00a0<\/strong><\/h3>\n<p data-sourcepos=\"76:1-76:105\">To automatically remove a container upon its exit, include the <code class=\"\">--rm<\/code> flag with the <code class=\"\">docker run<\/code> command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker run --rm image_name<\/code><\/pre>\n<\/div>\n<h3 id=\"h-removing-exited-containers-nbsp\" class=\"wp-block-heading\"><strong>Removing <em>Exited<\/em> Containers<\/strong><\/h3>\n<p data-sourcepos=\"82:1-82:184\">To remove only exited containers, use the <code class=\"\">-f status=exited<\/code> filter with the <code class=\"\">docker ps<\/code> command to identify them. Then, use the <code class=\"\">-q<\/code> flag to pass their IDs to the <code class=\"\">docker rm<\/code> command:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker ps -a -f status=exited\r\ndocker rm $(docker ps -a -f status=exited -q)<\/code><\/pre>\n<\/div>\n<h3 id=\"h-removing-containers-using-filters\" class=\"wp-block-heading\"><strong>Removing Containers <em>Using Filters<\/em><\/strong><\/h3>\n<p data-sourcepos=\"89:1-89:146\">To combine Docker filters, repeat the <code class=\"\">-f<\/code> flag and add additional values. For instance, to remove containers based on name or exited status, use:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker ps -a -f status=exited -f name=[Name]\r\ndocker rm $(docker ps -a -f status=exited -f name=[Name] -q)<\/code><\/pre>\n<\/div>\n<h3 id=\"h-deleting-containers-based-on-a-particular-pattern\" class=\"wp-block-heading\"><strong>Remove Containers <em>Based on a Particular Pattern<\/em><\/strong><\/h3>\n<p data-sourcepos=\"96:1-96:161\">To remove containers matching a specific pattern, combine <code class=\"\">docker ps<\/code> with <code class=\"\">grep<\/code> to identify them. Then, use <code class=\"\">awk<\/code> and <code class=\"\">xargs<\/code> to pass their IDs to <code class=\"\">docker rm<\/code>:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker ps -a | grep <span class=\"hljs-string\">\"pattern\"<\/span> | awk <span class=\"hljs-string\">'{print $1}'<\/span> | xargs docker rm<\/code><\/pre>\n<\/div>\n<h3 id=\"h-stop-and-remove-all-containers-nbsp\" class=\"wp-block-heading\"><strong>Stop and Remove all Containers\u00a0<\/strong><\/h3>\n<p data-sourcepos=\"102:1-102:216\">To stop and remove all containers, use the <code class=\"\">docker ps -a<\/code> command to view all containers, including those that are not running. Then, use the <code class=\"\">-q<\/code> flag to pass their IDs to the <code class=\"\">docker stop<\/code> and <code class=\"\">docker rm<\/code> commands:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">docker stop $(docker ps -a -q)\r\ndocker rm $(docker ps -a<\/code><\/pre>\n<\/div>\n<h2 id=\"h-conclusion\" class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n<p>In conclusion, this guide has provided comprehensive instructions for effectively managing Docker resources, including removing unnecessary images, containers, and volumes. By implementing these techniques,<span class=\"animating\"> you can optimize your Docker environment,<\/span><span class=\"animating\"> ensuring efficient disk usage and a well-organized system.<\/span><span class=\"animating\"> Remember to save this guide as a cheat sheet for future reference!<\/span><\/p>\n<\/div>\n<p>If you need to know additional information such as how to install Docker on Ubuntu, you can <a href=\"https:\/\/www.servergigabit.com\/guide\/kb\/install-docker-on-ubuntu\">refer this article<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker is a popular tool for managing containerized applications. However, over time, unused Docker images, volumes, and containers can accumulate on your system, consuming valuable disk space. This guide provides a comprehensive overview of the various commands and techniques for removing unused Docker elements, ensuring an efficient and organized Docker environment. Removing unused Images, Containers, Volumes and Networks The docker&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"kbtopic":[1099],"kbtag":[1152,1218,1217,1219],"class_list":["post-3574","kb","type-kb","status-publish","hentry","kbtopic-tutorials","kbtag-containers","kbtag-image","kbtag-removing-unused","kbtag-volumes"],"_links":{"self":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/comments?post=3574"}],"version-history":[{"count":6,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3574\/revisions"}],"predecessor-version":[{"id":6350,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3574\/revisions\/6350"}],"wp:attachment":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/media?parent=3574"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=3574"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtag?post=3574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}