Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Is there a way to automatically have a folder deleted every day at 4am?

I'm running 10.6.7.

share|improve this question
    
Related: apple.stackexchange.com/questions/3030/… –  user588 Apr 3 '11 at 17:51
add comment

2 Answers

up vote 18 down vote accepted

Appplescript & iCal

Open Applescript and enter the following code:

tell application "Finder"
  delete folder "folder" of home
end tell

Replacing folder with the folder you want to delete, and save the file.

If the folder you want to delete is outside home directory, (for example the folder /Users) then replace the delete line with:

delete folder "Users" of startup disk

Then open iCal and create a new recurring event at the time you want and as an alarm choose Run Script and select the applescript you created.


Cron

Open Terminal.app and enter:

crontab -e

There to the file opened, add the following line

0    4       *       *       *       rm -rf /Users/USER/folder

replacing /Users/USER/folder with the full path of your folder and save the file.


Launchd

Create a new text document, and paste the following code:

rm -rf /Path/to/Folder

Let's call it script.sh

Based on this article, create another new text document and paste the following:

<?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>Recurring Folder Deletion</string>
  <key>UserName</key>
  <string>nuc</string>
  <key>Program</key>
  <string>/path/to/script.sh</string>
  <key>StartCalendarInterval</key>
  <dict>
    <key>Minute</key>
    <integer>0</integer>
    <key>Hour</key>
    <integer>4</integer>
  </dict>
  <key>Debug</key>
  <false/>
  <key>AbandonProcessGroup</key>
  <true/>
</dict>
</plist>

Replace nuc with your account username, and /path/to/script.sh with the full path of the script we created on first step.

Save the file in /Library/LaunchDaemons/ as my_rfd.plist.

Finally, launch Terminal.app and type:

launchctl load /Library/LaunchDaemons/my_rfd.plist
share|improve this answer
    
Really nice answer. +1 for giving two explanations. –  moonglum Apr 3 '11 at 14:57
    
The folder isn't in my home directory - how can I delete a folder from the root of a hard drive named "Users"? –  Ryan Apr 4 '11 at 4:47
    
@ryan edited answer to include your request. –  nuc Apr 4 '11 at 7:21
    
Considering the unknown future state of cron, It would be wonderful if you would add a launchd method of doing this as well. –  Jason Salaz Apr 4 '11 at 18:53
1  
@vxjasonxv launchd way added! :) –  nuc Apr 5 '11 at 8:22
show 2 more comments

Hazel is a great app for doing this type of thing. It's not free, but it works great.

I use it to "Label" or highlight files that are have been downloaded in the last 24 hours, then to delete files from my downloads folder that are more than 7 days old.

I then have it empty my trash once a month.

This is just the basics of what can be done with Hazel, it's cool.

[I have no financial interest in Hazel]

share|improve this answer
    
Thanks - I have Hazel too. I also posted there trying to figure this out: bit.ly/dISt3G –  Ryan Apr 4 '11 at 20:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.