<ol>
<li><p>create a new file in the Cron file to house our command</p>
<pre><code> cd /etc/cron.daily
 sudo nano docker-prune-daily
</code></pre>
</li>
<li><p>Once the file opens add the prune command we created and inform the OS it's a bash file</p>
<pre><code>#!/bin/bash
docker image prune　 --force --filter “until=2020-01-01T00:00:00”
docker container prune　 --force --filter “until=2020-01-01T00:00:00”
</code></pre>
</li>
<li><p>Once you've added the command, hit Ctrl + O and then Ctrl + X to save and close the file</p>
</li>
<li><p>Now that we've created the file we need to tell the operating system that it needs to be executable with the following command:</p>
<pre><code>sudo chmod +x /etc/cron.daily/docker-prune-daily
</code></pre>
</li>
</ol>
<p>** If you want to test out to see if it will execute correctly next time the cronjob runs, you can force the daily Cron register to run with the following command:</p>
<pre><code>    run-parts /etc/cron.daily
</code></pre>

CC BY-SA 4.0
2022-03-21T15:40:28.757Z
2022-03-21T15:40:28.757Z
Vineesh Vijayan
<p>You can add a new cron entry by prompting:</p>
<p><code>sudo crontab -e</code></p>
<p>You can then edit the file with your command and its frequency. Every three months is not a standard cron frequency (i.e. there's no built-in solution to transcribe this into cron) so you can use these frequencies:</p>
<p>30 03 01 Jan,Apr,Jul,Oct *</p>
<p>or else</p>
<p>30 03 01 */3 *</p>
<p>(Based on <a href="https://serverfault.com/a/129636/432407">this answer</a> by Richard Holloway on ServerFault)</p>
<p>So eventually your cron entry will look like:</p>
<p><code>30 03 01 Jan,Apr,Jul,Oct * docker image prune　 --force --filter “until=2020-01-01T00:00:00” </code></p>
<p>You can check out your new entry by prompting:</p>
<p><code>sudo crontab -l</code></p>
<p><strong>Edit</strong></p>
<p>chicks's comment pointed out that the goal wasn't to automate an action to be performed every three months but rather to automatically find which objects are older than  months. So guessing that you would like to perform this action everyday, I would rather go for:</p>
<p><code>0 0 3 * * find /var/lib/docker -type f -ctime +90 -delete</code></p>
<p>This finds all Docker files older than three months and deletes them every day at 3AM.</p>

CC BY-SA 4.0
2022-03-18T17:10:22.237Z
I think the OP is asking for the docker to prune anything older than 3 months from now.  The cron job could run every day, but the three month window keeps moving.  Your cron runs every 3 months, but it looks like it prunes before the same date each time.
chicks
CC BY-SA 4.0
2022-03-18T18:10:49.073Z
Oh, right! Well spotted. I would use a script for that, finding out from the current date what "three months ago" is, and putting that script into the crontab. I'll edit my answer with this when I'm home from work
avazula
CC BY-SA 4.0
2022-03-18T15:51:33.530Z
2022-03-23T08:10:47.610Z
2022-03-23T08:10:47.610Z
avazula
<p>Can someone help me on how to clean up containers and images which are 3 months older automatically by using crontab scheduling?
Which type of method need to use for automation?
I have manually tried deleting by using below commands and it worked but I need to automate it, to perform action for every 3 months without manual action.</p>
<pre><code>docker image prune　 --force --filter “until=2020-01-01T00:00:00”
docker container prune　 --force --filter “until=2020-01-01T00:00:00”
</code></pre>

<docker><docker-compose>
How to automate docker prune commands using crontab
Hackaholic
# How to automate docker prune commands using crontab

(Asked by: username_0 on 2022-03-18)

Can someone help me on how to clean up containers and images which are 3 months older automatically by using crontab scheduling?
Which type of method need to use for automation?
I have manually tried deleting by using below commands and it worked but I need to automate it, to perform action for every 3 months without manual action.
docker image prune　 --force --filter “until=2020-01-01T00:00:00”
docker container prune　 --force --filter “until=2020-01-01T00:00:00”

**Tags:** docker, docker-compose


---

## 2 answers

### Answer by username_1 on 2022-03-21. Score: 2

create a new file in the Cron file to house our command
 cd /etc/cron.daily
 sudo nano docker-prune-daily


Once the file opens add the prune command we created and inform the OS it's a bash file
#!/bin/bash
docker image prune　 --force --filter “until=2020-01-01T00:00:00”
docker container prune　 --force --filter “until=2020-01-01T00:00:00”


Once you've added the command, hit Ctrl + O and then Ctrl + X to save and close the file

Now that we've created the file we need to tell the operating system that it needs to be executable with the following command:
sudo chmod +x /etc/cron.daily/docker-prune-daily



** If you want to test out to see if it will execute correctly next time the cronjob runs, you can force the daily Cron register to run with the following command:
    run-parts /etc/cron.daily


---

### Answer by username_2 on 2022-03-18. Score: 1

You can add a new cron entry by prompting:

`sudo crontab -e`

You can then edit the file with your command and its frequency. Every three months is not a standard cron frequency (i.e. there's no built-in solution to transcribe this into cron) so you can use these frequencies:

30 03 01 Jan,Apr,Jul,Oct \*

or else

30 03 01 \*/3 \*

(Based on [this answer](https://serverfault.com/a/129636/432407) by Richard Holloway on ServerFault)

So eventually your cron entry will look like:

`30 03 01 Jan,Apr,Jul,Oct * docker image prune　 --force --filter “until=2020-01-01T00:00:00”`

You can check out your new entry by prompting:

`sudo crontab -l`

**Edit**

username_3's comment pointed out that the goal wasn't to automate an action to be performed every three months but rather to automatically find which objects are older than months. So guessing that you would like to perform this action everyday, I would rather go for:

`0 0 3 * * find /var/lib/docker -type f -ctime +90 -delete`

This finds all Docker files older than three months and deletes them every day at 3AM.

> **Comments:**
> **username_3**: I think the OP is asking for the docker to prune anything older than 3 months from now.  The cron job could run every day, but the three month window keeps moving.  Your cron runs every 3 months, but it looks like it prunes before the same date each time.  
> **username_2**: Oh, right! Well spotted. I would use a script for that, finding out from the current date what "three months ago" is, and putting that script into the crontab. I'll edit my answer with this when I'm home from work  

---

