Skip to content
Menu
Marius Serbanica – Tech Blog
  • My Tech Blog
  • About me
  • Contact Me
  • Curriculum Vitae
  • Projects
  • Current Projects List
  • Certifications
  • Home Lab
  • Self-Hosted
  • Linux Cheat Sheet
  • Linux Commands
  • Privacy Policy
  • Site Map
Marius Serbanica – Tech Blog
June 14, 2025June 14, 2025

Automation – How to Automatically Clean Up Unused Docker Images



Tired of constantly cleaning up unused Docker images that accumulate over time? Here’s a simple and effective solution.

Login to your server terminal where you’re hosting docker

You can add a prune job on your Docker host that:

Runs automatically at regular intervals (e.g., daily/weekly)

Prunes all unused images

docker image prune -af --filter "until=24h"

sudo crontab -e
Bash

Add the following line to your cron

0 3 * * 0 docker image prune -af --filter "until=24h"
Bash

This will run every Sunday at 3 am and will clean all your unused images!

If you want to take it a step further you can have it daily, just add the following line to your cron

0 3 * * * docker image prune -af --filter "until=24h"
Bash

Want something more flexible?

Use a Docker-based cron container to do this for you, without touching the host cron:

# docker-compose.pruner.yml
version: "3.8"
services:
  docker-prune:
    image: docker
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    entrypoint: >
      sh -c "while true; do
        docker image prune -af --filter 'until=24h';
        sleep 86400;
      done"
YAML

Deploy with:

docker system prune -af --volumes
Bash

Just beware…
This won’t remove active images.

Use --filter "until=24h" to avoid pruning newly pulled images too soon.

If you ever want to do full cleanup (volumes, containers, networks), use:

Share on Social Media
x facebook linkedin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join my LinkedIn Network.

Recent Posts

  • Automation – How to Automatically Clean Up Unused Docker Images
  • PHP-Fusion 7 CMS fully dockerized!
  • Kubernetes cluster. Why and how
  • Installing GNU-World on ircu2
  • Replacing Cloudflare Tunnel with Tailscale on a VPS

Archives

  • June 2025
  • February 2025
  • January 2025
  • October 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023

Categories

  • How-To
  • Tech Industry
  • Tech, but personal
  • Tutorials

Recent Comments

  1. IRC Lamer on Installing GNU-World on ircu2
  2. severus2231 on Transforming a Mini PC into a Powerful Home Network Hub / Router/ Firewall with OPNsense
  3. admin on Mounting a NAS (Network Attached Storage) device on Linux
  4. abL on Mounting a NAS (Network Attached Storage) device on Linux
  5. Alin R on Cleaning up your Linux OS.
Social Media
Find me on social media
Facebook Twitter Instagram LinkedIn

©2025 Marius Serbanica – Tech Blog