<p>We have a script which we run periodicaly which scans our docker registry for orphaned items, and then deletes them.</p>

<p>Unfortunately, it requires multiple API calls to get all the data required, and then to delete each image.  But basically, if an image hash is lacking a 'latest' tag or a tag that starts with 'v' it gets culled.</p>

<p>You could probably also try deleting based on last modified date.</p>

<p>We run this as a cron job in kubernetes, but you could just as easily run a script like this as a periodic jenkins job.</p>

CC BY-SA 4.0
2019-09-09T07:00:23.203Z
2019-09-09T07:00:23.203Z
avi
<p>This is a very complicated issue for a few reasons:</p>

<ol>
<li><p>The biggest problem is Jenkins does not have any way of notifying you when a branch is deleted or a OR is merged. There are a lot of issues in Jenkin's jira, but from what I know - <a href="https://issues.jenkins-ci.org/browse/JENKINS-40606" rel="nofollow noreferrer">this</a> one is the monitored one.</p></li>
<li><p>You would somehow need to keep a record of all of the published tags.</p></li>
</ol>

<p>Overall I don't see how you'd achieve this as it's simply not possible from Jenkin's side. It would be cool if somebody else has found a solution to this. I have looked for one a few times already without any luck.</p>

CC BY-SA 4.0
2019-08-08T20:14:08.470Z
I see, would this be possible in gitlab?
DaSteph
CC BY-SA 4.0
2019-08-09T17:44:02.500Z
Unfortunately I don't use GitLab very much. Maybe you could hook into the [Stop an environment](https://docs.gitlab.com/ce/ci/environments.html#stopping-an-environment).
tftd
CC BY-SA 4.0
2019-08-08T15:15:14.843Z
2019-08-08T15:15:14.843Z
tftd
<p>I have a jenkins multi branch project, that builds on every commit and pushes the image to our private registry:</p>

<pre><code>docker-reg:5000/&lt;project&gt;/&lt;branch&gt;:latest
docker-reg:5000/&lt;project&gt;/&lt;branch&gt;:&lt;build-server&gt;-&lt;build-number&gt;
</code></pre>

<p>How can I delete the labels in the docker registry in order to let the garbage collector kick in upon:</p>

<ul>
<li>build gets orphaned after some time 

<ul>
<li>remove <code>&lt;project&gt;/&lt;branch&gt;:&lt;build-server&gt;-&lt;build-number</code></li>
</ul></li>
<li>branch gets merged and jenkins sub project gets automatically deleted

<ul>
<li>remove <code>&lt;project&gt;/&lt;branch&gt;:latest</code></li>
</ul></li>
</ul>

<docker><jenkins><docker-registry>
How to clean up old images in docker registry with jenkins
DaSteph
# How to clean up old images in docker registry with jenkins

(Asked by: username_0 on 2019-08-08)

I have a jenkins multi branch project, that builds on every commit and pushes the image to our private registry:
docker-reg:5000//:latest
docker-reg:5000//:-

How can I delete the labels in the docker registry in order to let the garbage collector kick in upon:

build gets orphaned after some time 


remove /:-

branch gets merged and jenkins sub project gets automatically deleted


remove /:latest

**Tags:** docker, jenkins, docker-registry


---

## 2 answers

### Answer by username_1 on 2019-09-09. Score: 1

We have a script which we run periodicaly which scans our docker registry for orphaned items, and then deletes them.

Unfortunately, it requires multiple API calls to get all the data required, and then to delete each image. But basically, if an image hash is lacking a 'latest' tag or a tag that starts with 'v' it gets culled.

You could probably also try deleting based on last modified date.

We run this as a cron job in kubernetes, but you could just as easily run a script like this as a periodic jenkins job.


---

### Answer by username_2 on 2019-08-08. Score: 0

This is a very complicated issue for a few reasons:

1. The biggest problem is Jenkins does not have any way of notifying you when a branch is deleted or a OR is merged. There are a lot of issues in Jenkin's jira, but from what I know - [this](https://issues.jenkins-ci.org/browse/JENKINS-40606) one is the monitored one.
2. You would somehow need to keep a record of all of the published tags.

Overall I don't see how you'd achieve this as it's simply not possible from Jenkin's side. It would be cool if somebody else has found a solution to this. I have looked for one a few times already without any luck.

> **Comments:**
> **username_0**: I see, would this be possible in gitlab?  
> **username_2**: Unfortunately I don't use GitLab very much. Maybe you could hook into the [Stop an environment](https://docs.gitlab.com/ce/ci/environments.html#stopping-an-environment).  

---

