<p>On my local machine (mac) I have a little script I found that I run periodically which cleans all the excess images up <a href="https://gist.github.com/MrTrustor/e690ba75cefe844086f5e7da909b35ce" rel="nofollow noreferrer">clean-docker-for-mac.sh</a></p>

<p>For my servers I run <a href="https://hub.docker.com/r/meltwater/docker-cleanup/" rel="nofollow noreferrer">meltwater/docker-cleanup</a> which periodically cleans up exited containers and removes images and volumes that aren't in use.</p>

<p>I use a lot of different Docker images for my work on Codemason that scripts like these are necessity for me. Enjoy!</p>

CC BY-SA 3.0
2017-11-16T00:44:17.140Z
2017-11-16T00:44:17.140Z
bmagg
<p>Can't call it's the best practice but this is what we use triggered by cron, happy to see better suggestions.</p>

<pre><code>echo "safely removing untagged images"
docker rmi $(docker images | awk '/&lt;none&gt;/{print $3}')

echo "safely removing stopped containers"
docker rm $(docker ps -a -q)

echo "safely removing old containers"
docker ps -a | awk '/weeks ago|months ago|days ago/{print $1}' | xargs --no-run-if-empty docker rm

echo "safely removing old images"
docker images | awk '/weeks ago|months ago|days ago/{print $3}' | xargs --no-run-if-empty docker rmi

echo "safely removing old volumes, custom rebuild of martin/docker-cleanup-volumes image"
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm example/docker-cleanup-volumes

echo "native cleanup Docker =&gt; 12"
docker system prune -f
</code></pre>

CC BY-SA 3.0
2017-03-27T19:14:43.910Z
Please don't chain grep and awk, do the same filter just before the awk command ` regex { command }`
Tensibai
CC BY-SA 3.0
2017-03-27T20:32:13.870Z
@Tensibai anything to make AWK wizards happy :)
rombob
CC BY-SA 3.0
2017-03-27T20:53:24.797Z
Just in case, that's not just pedantic , you save process spawn and a large bunch of context changes :) It doesn't change much here, but any occasion to advocate for it is good :)
Tensibai
CC BY-SA 3.0
2017-03-27T20:57:09.983Z
@Tensibai definitely, good habits pay off with time, spawns cost us time and money :)
rombob
CC BY-SA 3.0
2017-03-27T20:59:06.677Z
Specially when you parse/format billions lines log files to prepare for R input :) Here I don't expect more than a few dozen lines.
Tensibai
CC BY-SA 3.0
2017-03-27T21:48:17.767Z
@Tensibai yep, all these cool new technologies for big data out there, and yet good old awk is still relevant :)
rombob
CC BY-SA 3.0
2017-03-28T09:01:05.687Z
Variation for first command:  `for i in $(docker images --filter dangling=true -q); do docker rmi "$i"; done` unsure it can replace the `docker images | awk` part.
Tensibai
CC BY-SA 3.0
2017-03-27T16:59:52.407Z
2017-03-27T20:36:44.190Z
2017-03-27T20:36:44.190Z
rombob
<p><code>
docker system prune --volumes
</code></p>

<p>Is the way to do this since v17. <a href="https://docs.docker.com/engine/reference/commandline/system_prune/" rel="nofollow noreferrer">Docs here</a></p>

CC BY-SA 3.0
2017-12-19T09:48:28.483Z
2017-12-19T09:48:28.483Z
Mbrevda
<p>I've had good luck with
<a href="https://github.com/spotify/docker-gc" rel="noreferrer">Spotify/docker-gc</a>. It can be run from cron or as a docker container.</p>

<blockquote>
  <p>A simple Docker container and image garbage collection script.</p>
  
  <ul>
  <li>Containers that exited more than an hour ago are removed.</li>
  <li>Images that don't belong to any remaining container after that are removed.</li>
  </ul>
</blockquote>

CC BY-SA 3.0
2017-11-16T14:39:04.077Z
At first glance, it seems to be, behind all the bash stuff, more or less a subset of the one-liners from @rombob 's answer. Is that true, or is there some "intelligence" in `docker-gc` that I am missing?
AnoE
CC BY-SA 3.0
2018-04-19T10:53:15.753Z
Op links and asks a question on how to clean from the registry, but docker-gc doesn't clean from the registry, it just cleans from your local machine. I guess OP was really asking how to clean up locally
dalore
CC BY-SA 4.0
2019-09-04T09:24:08.173Z
Docker-gc's page now recommends to use `docker system prune` instead. But I didn't find how to exclude some images from removing with Docker's prune. It there a way to exclude some images with `docker system prune`?
Alexander Artemenko
CC BY-SA 3.0
2017-03-29T12:32:17.797Z
2017-03-29T12:32:17.797Z
Ahmed Elsabbahy
<p>Is there an elegant way or a best practice when it comes to deleting old images from the Docker registry?</p>

<p>I see a lot of requests/issues here: <a href="https://github.com/docker/docker-registry/labels/delete" rel="noreferrer">https://github.com/docker/docker-registry/labels/delete</a>, but didn't find a good/popular solution for it.</p>

<p><strong>So, is there a tool or a technique which would help me do that?</strong></p>

<p>Also, is there any best practices which you follow while doing it?</p>

CC BY-SA 3.0
2018-04-19T10:54:03.333Z
Are you asking how to clean the registry? (Which is the link and question you provide), but the accepted answer is not cleaning the registry, but cleaning your local docker.
dalore
<docker><toolchain>
Are there any best practices and/or tools for cleaning old Docker images on a regular basis?
Dawny33
# Are there any best practices and/or tools for cleaning old Docker images on a regular basis?

(Asked by: username_0 on 2017-03-27)

Is there an elegant way or a best practice when it comes to deleting old images from the Docker registry?

I see a lot of requests/issues here: <https://github.com/docker/docker-registry/labels/delete>, but didn't find a good/popular solution for it.

**So, is there a tool or a technique which would help me do that?**

Also, is there any best practices which you follow while doing it?

**Tags:** docker, toolchain

> **Comments:**
> **username_1**: Are you asking how to clean the registry? (Which is the link and question you provide), but the accepted answer is not cleaning the registry, but cleaning your local docker.  

---

## 4 answers

### Answer by username_6 on 2017-03-29. Score: 10 | Accepted answer

I've had good luck with
[Spotify/docker-gc](https://github.com/spotify/docker-gc). It can be run from cron or as a docker container.

> A simple Docker container and image garbage collection script.
>
> * Containers that exited more than an hour ago are removed.
> * Images that don't belong to any remaining container after that are removed.

> **Comments:**
> **username_7**: At first glance, it seems to be, behind all the bash stuff, more or less a subset of the one-liners from @username_3 's answer. Is that true, or is there some "intelligence" in `docker-gc` that I am missing?  
> **username_1**: Op links and asks a question on how to clean from the registry, but docker-gc doesn't clean from the registry, it just cleans from your local machine. I guess OP was really asking how to clean up locally  
> **username_8**: Docker-gc's page now recommends to use `docker system prune` instead. But I didn't find how to exclude some images from removing with Docker's prune. It there a way to exclude some images with `docker system prune`?  

---

### Answer by username_3 on 2017-03-27. Score: 9

Can't call it's the best practice but this is what we use triggered by cron, happy to see better suggestions.
echo "safely removing untagged images"
docker rmi $(docker images | awk '//{print $3}')

echo "safely removing stopped containers"
docker rm $(docker ps -a -q)

echo "safely removing old containers"
docker ps -a | awk '/weeks ago|months ago|days ago/{print $1}' | xargs --no-run-if-empty docker rm

echo "safely removing old images"
docker images | awk '/weeks ago|months ago|days ago/{print $3}' | xargs --no-run-if-empty docker rmi

echo "safely removing old volumes, custom rebuild of martin/docker-cleanup-volumes image"
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm example/docker-cleanup-volumes

echo "native cleanup Docker => 12"
docker system prune -f

> **Comments:**
> **username_4**: Please don't chain grep and awk, do the same filter just before the awk command ` regex { command }`  
> **username_3**: @username_4 anything to make AWK wizards happy :)  
> **username_4**: Just in case, that's not just pedantic , you save process spawn and a large bunch of context changes :) It doesn't change much here, but any occasion to advocate for it is good :)  
> **username_3**: @username_4 definitely, good habits pay off with time, spawns cost us time and money :)  
> **username_4**: Specially when you parse/format billions lines log files to prepare for R input :) Here I don't expect more than a few dozen lines.  
> **username_3**: @username_4 yep, all these cool new technologies for big data out there, and yet good old awk is still relevant :)  
> **username_4**: Variation for first command:  `for i in $(docker images --filter dangling=true -q); do docker rmi "$i"; done` unsure it can replace the `docker images | awk` part.  

---

### Answer by username_2 on 2017-11-16. Score: 4

On my local machine (mac) I have a little script I found that I run periodically which cleans all the excess images up [clean-docker-for-mac.sh](https://gist.github.com/MrTrustor/e690ba75cefe844086f5e7da909b35ce)

For my servers I run [meltwater/docker-cleanup](https://hub.docker.com/r/meltwater/docker-cleanup/) which periodically cleans up exited containers and removes images and volumes that aren't in use.

I use a lot of different Docker images for my work on Codemason that scripts like these are necessity for me. Enjoy!


---

### Answer by username_5 on 2017-12-19. Score: 0

`docker system prune --volumes`

Is the way to do this since v17. [Docs here](https://docs.docker.com/engine/reference/commandline/system_prune/)


---

