Automate Screenshot Deletion on a Mac

A guide on how to set up your Mac to automatically clean up your screenshot mess once and for all. It only is a one time task that saves time and reduces stress in your everyday life.

Desktop screen with a lot of screenshots

Although taking screenshots belongs to one of the most beloved features on a Mac, its default behaviour is not very convenient, as it adds all the screenshots to your desktop — which you want to keep clean and tidy. Way too often, taking screenshots results in a desktop which looks like someone lost control over their life.

A few years ago, I wrote a small script to make the computer deal with this issue automatically. Since then, I’ve set up the same script on computers of many friends and colleagues, so it’s about time to make it public. In this guide, you’ll learn how to do the following things:

  • Set up a custom directory where your screenshots will be saved to
  • Set up a script which automatically deletes old screenshots

No technical skills required. But you need a computer using Mac OS.

Custom directory for your screenshots

If you have a lot of files on your desktop, it might be hard to quickly find what you need as they probably aren’t sorted in any way. So let‘s move your current and future screenshots into a dedicated directory first.

Create a folder wherever you want all your screenshots to be and put your saved screenshots in there. If you want to be able to reach them quickly, drag the folder into the Finder sidebar to add it to your favourites. This is especially handy if you send them around often as the sidebar is also present in the file selection prompt.

Screenshots directory

Now we need to tell the operating system to save your future screenshots into this directory. Hit cmd + spacebar to open Spotlight Search and type in Terminal. Open the app by pressing enter, then type

defaults write com.apple.screencapture location 

(don’t forget the trailing space) into the Terminal window and drag and drop your Screenshots folder into the window. The result should look like this:

Terminal with command input
Terminal with command input

Hit enter to confirm. Now all new screenshots should automatically be saved into your custom directory. Try it out by taking a random screenshot!

If you want to change the filename from “Screen Shot” to “Screenshot” like in my example, just input defaults write com.apple.screencapture name "Screenshot" into the Terminal and confirm with enter.

Automatically delete old screenshots

So far we only moved the problem to another place. Handling screenshots in a dedicated directory should be easier than handling them together with other files on your desktop, but now it’s even easier to forget to clean them up!

We’ll use a Launch Agent to clean up the directory for us. A Launch Agent is a program triggered based on specific events. In our case we want to trigger a command which deletes screenshots older than a month once a day.

Open Spotlight Search again and open the app TextEdit. Very important: After an empty TextEdit window appears, go to the menu bar and click Format → Make Plain Text. If you see Make Rich Text instead, you’re good to go.

Now paste this code into your TextEdit window:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.clean-screenshots-dir</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>find ~/Pictures/Screenshots/Screenshot* -mtime +30 -exec mv {} ~/.Trash \;</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<key>KeepAlive</key>
<dict>
<key>PathState</key>
<dict>
<key>~/Pictures/Screenshots</key>
<true/>
</dict>
</dict>
</dict>
</plist>

Try to read through the code, you might be able to guess some details here and there. You’ll need to customise a few lines.

<string>com.user.clean-screenshots-dir</string>

This represents the name of the Launch Agent you’re about to create. Feel free to change user to your own name, but don’t use spaces, dots or other special characters.

<string>find ~/Pictures/Screenshots/Screenshot* -mtime +30 -exec mv {} ~/.Trash \;</string>

This is the command we want to execute. It searches for files starting with Screenshot in the directory ~/Pictures/Screenshots (~ stands for your user directory) which were created at least 30 days ago. Then it moves all these files it found to the Trash. Change the directory to your specific directory and the time to something you find appropriate. For example, the command

find ~/Desktop/Screenshot* -mtime +365 -exec mv {} ~/.Trash \;

moves all screenshots on the Desktop created at least 365 days ago to the Trash.

If your file name contains spaces like the default “Screen Shot”, you need to prepend each space with a \ to make it work → Screen\ Shot.

After you specified your own find command, you can test it by pasting everything before -exec into a Terminal window like this:

find ~/Pictures/Screenshots/Screenshot* -mtime +30

Press enter, the Terminal now outputs the paths to all screenshots your Launch Agent would move to the Trash.

<key>RunAtLoad</key>

This parameter specifies to run this program when you log in.

<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>0</integer>
</dict>

This part defines based on which event the program should be triggered. Don’t worry, StartCalendarInterval doesn’t interfere with your calendar, it’s just a way to express that you want to trigger this program every day at a set time: in this example at noon. The time doesn’t really matter because the operating system will run the command the next time you use your computer if it missed a date. Put in any time you feel comfortable with.

<key>KeepAlive</key>
<dict>
<key>PathState</key>
<dict>
<key>~/Pictures/Screenshots</key>
<true/>
</dict>
</dict>

Finally, we tell the operating system to keep the Launch Agent on as long as the Screenshot directory exists. Replace the path ~/Pictures/Screenshots with your own Screenshot directory.

Make sure you don’t have any typos and then save the file as the specified name you already gave it and add the extension .plist. In my case the filename is com.user.clean-screenshot-dir.plist.

Then open a new Finder window and click on Go → Go to Folder… in the menu bar or use the shortcut cmd + shift + G, then enter ~/Library into the prompt. This is a hidden directory, so you won’t find it by regular browsing in the Finder. In this directory should be a folder called LaunchAgents. If there is none, create it. Place your Launch Agent file directly into the LaunchAgents folder, so its path is similar to following:

~/Library/LaunchAgents/com.user.clean-screenshot-dir.plist

Congratulations, you created your own custom Launch Agent! It will automatically start when you restart your computer. If you don’t want to restart, you can also start the Launch Agent manually by opening the Terminal again and executing following command with the corresponding path to your Launch Agent:

launchctl load ~/Library/LaunchAgents/com.user.clean-screenshot-dir.plist

To check if your script works, take a look into the Trash. There should be all files which match your specified deletion pattern.

Trash with lots of screenshots in it
Trash with lots of screenshots in it

Now your screenshots will be moved to the Trash automatically after whatever time you specified. If you’ve set up to remove items from the Trash after 30 days in the Finder Preferences, the screenshots will be removed completely after being 30 days in the Trash and you don’t need to do anything to keep your hard drive clean.

If you’re interested into Launch Agents to automate more stuff, check out this tutorial or the Apple Documentation.

Help! I need to undo all of this!

Simply delete the Launch Agent file and restart to stop automatically moving screenshots to the Trash. To reset the default directory for your screenshots, run following command in the Terminal:

defaults write com.apple.screencapture location ~/Desktop

There are many small things in our daily lives which distract and annoy us. In isolation they’re too small to be cared about, but they add up. I definitely noticed a relief after not having to think about such a repetitive task like managing screenshots anymore.

Empty desktop screen

Writing code for the internet

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store