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”

2 Answers 2

  1. create a new file in the Cron file to house our command

     cd /etc/cron.daily
     sudo nano docker-prune-daily
  2. Once the file opens add the prune command we created and inform the OS it's a bash file

    docker image prune  --force --filter “until=2020-01-01T00:00:00”
    docker container prune  --force --filter “until=2020-01-01T00:00:00”
  3. Once you've added the command, hit Ctrl + O and then Ctrl + X to save and close the file

  4. 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

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 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


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:

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.

  • 1
    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
    Commented Mar 18, 2022 at 17:10
  • 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
    Commented Mar 18, 2022 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.