Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Whether you still call it Trash or Bin, it's the same concept - deleted files are not permanently deleted but are made inaccessible until you decide to permanently remove them. Sometimes this is a little bit bothersome, especially if you happen to be using your Mac as a media centre/HTPC. So here's how to set it up so that the Trash is emptied automatically at certain intervals and times.

Note if you are content with having the Trash emptied once every 30 days, you can do so from Finder preferences...
emptytrash_1.png


I wanted to remove unneeded files a bit more often, so here's how I did it...

Step 1 - Open the Script Editor application from the Applications/Utilities folder.
emptytrash_2.png


Step 2 - Enter the following code into the AppleScript editor and click the compile button (the hammer)...
 AppleScript
tell application "Finder"
set warns before emptying of trash to false
empty the trash
set warns before emptying of trash to true
end tell


Step 3 - Save your code as an Application called EmptyTrash somewhere convenient (e.g. the Documents folder)...
emptytrash_4.png




Step 4 - Open the Calendar application...
emptytrash_5.png


Step 5 - Create a new event at a convenient time (when your Mac is likely to be on) and in the alert field set it to Custom > Open File > Other, and select the EmptyTrash application you created earlier.
emptytrash_6.jpg


Step 6 - Set the repeat interval (I picked every day). You can also create a new calendar and place this event there, then hide the calendar if you do not want this event to appear all over the place. I didn't do this because my Mac is only used as a media centre.
emptytrash_7.png


Once all that is done, your Mac's Trash will be emptied automatically at the time you set (assuming the Mac is on).

Update - Operation timed out

If you implement the above script and keep getting the operation timed out error, the best alternative is to switch to using shell commands to empty the trash. This is done by replacing the AppleScript from step 2 with the following...
 AppleScript
do shell script "/bin/bash -c 'rm -rf ~/.Trash/*'"


In addition, you have to give the EmptyTrash app you've created full disk access permissions by following this guide - How to fix 'Operation not permitted' error in macOS terminal and ~/.Trash.

-I

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.