I Got Tired of Cleaning My Mac's Temp Folder, So I Automated It

July 1, 2025 • 4 min read

We’ve all been there – that one folder that becomes a digital junk drawer, collecting files “we might need someday” but never actually touch. For most of us, it’s the Downloads folder, but it could be any directory that slowly grows into an organizational nightmare.

As developers, we deal with plenty of temporary files: downloaded dependencies, test data, build artifacts, random snippets we’ve grabbed for reference. Rather than manually cleaning house every few weeks (and inevitably forgetting), I’ve been using a simple automated solution for years that keeps a dedicated temp folder tidy without any manual intervention.

The Solution: LaunchAgents + A Simple Script

We’ll use macOS’s built-in launchd system to run a cleanup script on a schedule. Think of it as a lightweight, native alternative to cron that’s designed specifically for macOS.

Step 1: Create the LaunchAgent Configuration

First, let’s create a plist (property list) file that defines our scheduled task:

<?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>com.developer.deletetempfiles</string>
	<key>ProgramArguments</key>
	<array>
		<string>sh</string>
		<string>-c</string>
		<string>find ~/_temp/. -mtime +3 -exec mv {} ~/.Trash/ \;</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Minute</key>
		<integer>0</integer>
	</dict>
</dict>
</plist>

Breaking Down the plist Configuration

This XML file is essentially a configuration that tells macOS’s launchd daemon how and when to run our cleanup task. Here’s what each key does:

Label: This is a unique identifier for your launch agent. Think of it like a service name – it needs to be unique across your system. The reverse-domain notation (like com.developer.deletetempfiles) is conventional but not required.

ProgramArguments: This array defines the command to execute. We’re using:

  • sh - the shell interpreter
  • -c - tells the shell to execute the following string as a command
  • The actual find command (more on this below)

StartCalendarInterval: This defines when the task runs. By specifying only Minute: 0, we’re telling it to run at the top of every hour (12:00, 1:00, 2:00, etc.). You could also add Hour, Day, Month, etc. for more specific scheduling.

Understanding the Find Command

Let’s break down what find ~/_temp/. -mtime +3 -exec mv {} ~/.Trash/ \; is doing:

  • find ~/_temp/. - Search within the _temp directory in your home folder
  • -mtime +3 - Find files modified more than 3 days ago (+3 means “greater than 3 days”)
  • -exec mv {} ~/.Trash/ \; - For each found file, execute mv (move) to relocate it to the Trash
  • {} is a placeholder that gets replaced with each found file’s path
  • \; terminates the -exec command

This approach moves files to Trash rather than permanently deleting them, so you still have a safety net if you realize you needed something.

Step 2: Install and Activate the LaunchAgent

Now let’s get this scheduled task set up:

# Create the LaunchAgents directory if it doesn't exist
mkdir -p ~/Library/LaunchAgents

# Copy your plist file to the LaunchAgents directory
cp com.developer.deletetempfiles.plist ~/Library/LaunchAgents/com.developer.deletetempfiles.plist

# Load and enable the launch agent
launchctl load -w ~/Library/LaunchAgents/com.developer.deletetempfiles.plist

What’s launchctl?

launchctl is the command-line interface for interacting with launchd, macOS’s service management framework. Think of launchd as the system’s process supervisor – it handles starting, stopping, and scheduling various system and user processes.

When you run launchctl load -w, you’re telling launchd to:

  • Load the configuration from your plist file
  • Enable it with the -w flag (which “writes” the enabled state, making it persistent across reboots)

Step 3: Create Your Temp Directory

Don’t forget to actually create the directory that will be monitored:

mkdir -p ~/_temp

Now you can drop files into ~/_temp knowing they’ll automatically be cleaned up after 3 days.

Managing Your LaunchAgent

To disable the scheduled cleanup:

# To disable the scheduled cleanup:
launchctl unload -w ~/Library/LaunchAgents/com.developer.deletetempfiles.plist

# To check if it's running:
launchctl list | grep com.developer.deletetempfiles

# To manually test the cleanup command:
find ~/_temp/. -mtime +3 -exec mv {} ~/.Trash/ \;

Customization Ideas

  • Change the schedule: Modify StartCalendarInterval to run daily at 2 AM by adding <key>Hour</key><integer>2</integer>
  • Adjust the age threshold: Change +3 to +1 for daily cleanup or +7 for weekly
  • Monitor different directories: Update the path in the find command
  • Permanent deletion: Replace mv {} ~/.Trash/ with rm {} if you want permanent deletion (use with caution!)

Resources