How To Automatically Clean Your macOS Downloads Folder

I download a lot of crap, and it ends up in my Downloads folder. I used to use Hazel to keep my Downloads and Trash clean, but it was overkill: Hazel is a much more powerful tool than I needed. With the relatively recent addition to macOS to automatically remove items from the trash after 30 days I was left with just my downloads folder.

So I wrote a script.

#!/usr/bin/env bash

folderName="${1:-$HOME/Downloads}"
olderThan="${2:-7}"

find "$folderName" -type f -ctime "+${olderThan}" -not -name '.\*' -exec rm '{}' ';'
find "$folderName" -type d -empty -not -name '.\*' -exec rmdir '{}' ';'

Then, I installed it as a regularly-running launchd agent.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>net.ardvaark.autocleandownloads</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/bvargas/Documents/Misc/AutoCleanDownloads/auto-clean-downloads.sh</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartCalendarInterval</key>
  <dict>
      <key>Hour</key>
      <integer>03</integer>
  </dict>
</dict>
</plist>

Which I then copied to ~/Library/LaunchAgents/net.ardvaark.autocleandownloads.plist, and installed with the command:

launchctl load ~/Library/LaunchAgents/net.ardvaark.autocleandownloads.plist

And now my Mac automatically cleans my download folder of anything older than a week every morning at three o’clock.