Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automatically removing trash 5 days old

Hi

I did a secure empty trash yesterday which I have not done in a few years and there were over 1.5 million files to permanently erase. This is dangerous to keep this much trash and it takes way too long to clean up. I would prefer regular maintenance . . . to securly and automatically empty the trash regularly and still maintain the benefit being able to prevent accidental deletion of recent work. The requirement therefore would be to perhaps during off hours, once a day or upon startup automtically secure empty trash only items that have aged in the trash at least x days.


Can this be done quickly and simply with applescript or a cron job?

Posted on Jul 12, 2011 12:58 AM

Reply
37 replies

Jul 12, 2011 2:04 AM in response to Hawaiian Scuba Dude

I'm looking for something simple like


find .Trashes -mtime +5 -exec rm {} \;


It should run in the background. The actual days is irrelevant (3-5), the rm command is not secure and I don't want to mess with any Apple reserved files in the .Trashes folder.

A scheduler option with aging added to the existing "Empty Trash" vehicle would be ideal.

Jul 12, 2011 10:31 AM in response to Hawaiian Scuba Dude

Here's a script and launchagent that will do what you want:


First, copy the following into the Applescript editor, and save it as dated_deleter.scpt in (say) /Library/Scripts. Set the security level and days to wait properties to what you want.


property keyPhrase : "Trashed on: "

property securityLevel : 2 -- overwrites: 1=1-pass, 2=7-pass, 3=35-pass, see man srm

property daysToWait : 7


tell application "Finder"


-- get files in trash that are not new, and check dates

set oldFiles to (every item of trash whose comment contains keyPhrase)

set trashmeFiles to ""

repeat with thisFile in oldFiles


-- extract trashed date from the spotlight comments

set trashedDate to last item of my tid(comment of thisFile, keyPhrase)

try


-- check dates in a try block in case of weirdness)

if my checkDate(trashedDate) then

set trashmeFiles to trashmeFiles & " " & my makePosix(thisFile as alias)

end if

end try

end repeat



-- trash 'em in a subroutine

my trashEm(trashmeFiles)



-- gather new files

set newFiles to every item of trash whose comment does not contain keyPhrase


-- add the current date to the spotlight comment of the new files

repeat with thisFile in newFiles

set c to comment of thisFile

if c = "" then

set comment of thisFile to keyPhrase & short date string of (current date)

else

set comment of thisFile to c & return & keyPhrase & short date string of (current date)

end if

end repeat

end tell


on trashEm(fs)


-- set up proper security level

if securityLevel = 1 then

set cmd to "srm -rfsz"

else if securityLevel = 3 then

set cmd to "srm -rfz"

else

set cmd to "srm -rfmz"

end if



-- start a secure delete process in the background

do shell script cmd & fs & " &> /dev/null &"

end trashEm


on tid(input, delim)


-- generic subroutine to handle text items

set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid


on makePosix(f)

return quoted form of POSIX path of f

end makePosix


on checkDate(d)

return (date d) ≤ (current date) - daysToWait * days

end checkDate


second copy the following into a plain text file (use TextWrangler, or TextEdit in plain-text mode, don't use rich text), modify the /path/to/dated_deleter.scpt line so that it is a POSIX path to the script (if you use the above, that would be /Library/Scripts/dated_deleter.scpt) and save it as user.trash.dated.plist in ~/Library/LaunchAgents or /Library/LaunchAgents. (use the former if there's just one user on the machine; use the latter if you want it to apply to multiple users)


<?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>user.trash.dated</string>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>/path/to/dated_deleter.scpt</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>


Finally, open terminal and enter the command launchctl load /Library/LaunchAgents/user.trash.dated.plist (or alternately just restart the machine). Either of these will load the plist into launchd as a job, launchd will wait until midnight of each day (hour 0, minute 0) and then run the dated_deleter script. Basically the script writes the date into the items spotlight comments the first time it sees it in the trash, and then deletes it if the written date gets too old.


Two caveats:

  • if you recover a file from the trash and then accidentally delete it again, it may be deleted next pass through unless you delete the key phrase from the spotlight comments. you can do that in the Finder get info window.
  • I noticed in testing that if I copy a file, run the deleter on it, delete it, and then copy the same file again for a second test, spotlight remembers the comments applied to the first file. Somehow Spotlight recognizes that the new copy is identical to the old copy and retains information from the old - completely unexpected. This might be an issue in odd cases if you do lots of file duplicating; if so we'll need to add in a routine that deletes the key phrase from spotlight comments before the file gets deleted.

Jul 13, 2011 9:37 AM in response to Hawaiian Scuba Dude

Hawaiian Scuba Dude wrote:


The requirement therefore would be to perhaps during off hours, once a day or upon startup automtically secure empty trash only items that have aged in the trash at least x days.


Can this be done quickly and simply with applescript or a cron job?


I'm looking for something simple like


find .Trashes -mtime +5 -exec rm {} \;


Sure, here's the find command (notice srm for secure rm):


This deletes trash 7 days old (based on create time).

Notes:

-- I use -mindepth 1 -prune, so that if a FOLDER is in the Trash, every file in the folder is deleted (-R), (even is it is less than 7 days old -- good for App bundles).

-- Some files might not be deleted if srm has an error (for example, write protected files owned by another user)

-- This only handles the Users Home Trash. If you have attached drives, adjust accordingly.


/usr/bin/find $HOME/.Trash -mindepth 1 -prune -not -newerct '7 days ago' -exec /usr/bin/srm -Rf {} \;


Now, just use launchd (or crom, if you prefer) to run once a day (this runs at 1am daily):

Place in ~/Library/LaunchAgents/


<?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.tony.EmptyTrash7DaysOld</string>
          <key>ProgramArguments</key>
          <array>
                    <string>/usr/bin/find</string>
                    <string>$HOME/.Trash</string>
                    <string>-mindepth</string>
                    <string>1</string>
                    <string>-prune</string>
                    <string>-not</string>
                    <string>-newerct</string>
                    <string>7 days ago</string>
                    <string>-exec</string>
                    <string>/usr/bin/srm</string>
                    <string>-Rf</string>
                    <string>{}</string>
                    <string>\;</string>
          </array>
          <key>QueueDirectories</key>
          <array/>
          <key>StartCalendarInterval</key>
          <dict>
                    <key>Hour</key>
                    <integer>1</integer>
                    <key>Minute</key>
                    <integer>0</integer>
          </dict>
          <key>WatchPaths</key>
          <array/>
</dict>
</plist>

Jul 13, 2011 4:01 PM in response to Linc Davis

Linc Davis wrote:


This is a fairly basic records retention requirement for any business.


What do you mean?


Any hopes of including it in the OS?


I sure hope not. The Trash is not for temporary file storage. It's a safeguard against accidental deletion. You should never put anything in the Trash unless you intend to delete it immediately.


True, but so is Mail, and we have a Preference (for POP) to "permanently erase deleted messages that are..." "one week old, one month old, never". This would be a nice preference for Trash. Apple could use 'never' as the default, and give us an option to automatically empty the trash every week, or month.

Jun 6, 2012 7:25 PM in response to Linc Davis

Records retention is knowing what types of electronic documents you have and the expected/desired/required life of each type is. When that item has passed its expected life it should be removed and know it is no longer available. For instance, temporary data should always be temporary. A final publication should be put into a vault and kept secure. If the rules are thought out, in place, and practiced, there is never a "what if I might need this" concern. Data is automatically not kept for an unwarranted, undesired amount of time.


To your point, there is some desired/required safe threshold from accidental deletion but at some point also know if it is expected to be gone, it should be gone.

Automatically removing trash 5 days old

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.