Docker Tip #32: Automatically Clean up after Docker Daily


Learn how to automatically remove dangling Docker images and other resources on a daily basis for Linux, Windows and MacOS.

The command we’re going to be executing is docker system prune -f which will remove all stopped containers, all unused networks, all dangling images and build caches.

We’ll want to automatically execute this command every day at 3AM, but how we do it will depend on what OS you’re using. So here’s how to do it on all major operating systems.


We have access to the cron utility, so this will be simple. Run crontab -e, pick an editor (nano) if you haven’t done so already and then add this line to the bottom and save it:

0 3 * * * /usr/bin/docker system prune -f

Docker is most likely installed at /usr/bin/docker but you can verify that by running which docker. Change the path if you need to.


I am running WSL as my primary Linux development environment but when you close your terminal, the WSL process ends, so there’s no way to reasonably depend on cron for WSL because you would need to keep your terminal open 24 / 7.

The steps below will work with or without WSL as long as you’re using Docker for Windows. If you are using Docker Toolbox you will need to change step 8 to launch the Docker Quickstart Terminal and pass in the Docker command to that as an argument.

Here’s how to do it using the built in Windows Task Scheduler:
  1. Search for “Computer Management” and run it
  2. Click “System Tools -> Task Scheduler” in the sidebar
  3. Click “Create Task” in the action bar on the right
  4. Name it “Docker-System-Prune”
  5. Change the security option to “Run whether user is logged on or not”
  6. Change “Configure for” (dropdown box) to “Windows 10”
  7. Goto the “Actions” tab and click “New”
  8. Enter in C:\Program Files\Docker\Docker\Resources\bin\docker.exe as the Program/script
  9. Add the arguments system prune -f and hit OK to anything it says
  10. Goto the “Triggers” tab and click “New”
  11. Change the settings to “Daily” and set the “Start” time to 3AM and recur every 1 day
  12. Click OK and then click OK again for Create Task
  13. Enter in your admin password


I don’t use a Mac but my buddy Scott does, so he provided me the steps listed below for LaunchD (a default scheduler on MacOS that is supposed to replace cron).

Create the LaunchD based schedule file:

sudo touch /Library/LaunchDaemons/DockerSystemPrune.plist

Add this content to the above file and save it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
<plist version="1.0">
    <string>/usr/local/bin/docker system prune -f</string>
Load the task into LaunchD:

launchctl load -w /Library/LaunchDaemons/DockerSystemPrune.plist

At this point it’s activated and you’re good to go.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.