You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Jun 9, 2013 10:00 AM in response to twtwtw

It not works for me and I can't understand where is the error.

The script works well, if I run it in Applescript editor, so I think, the problem is in .plist file created by me or somewhere else.

What I did:

I made dated_deleter.scpt file with content written above and placed it in ~/Library/Scripts folder. It works correct after manual execution, there are new tags in Spotlight field files properties in Trash folder.

Then I made user.trash.dated.plist file with content:



<?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>/Users/user1/Library/Scripts/dated_deleter.scpt</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>



and placed it in ~/Library/LaunchAgents folder.

Next I entered launchctl load ~/Library/LaunchAgents/user.trash.dated.plist in Terminal, launchctl list command shows user.trash.dated.plist is in list, status 1.

And… nothing. No new tags, the applescript is not executing.

Could somebody explain me, what's wrong?

Jul 16, 2013 5:36 PM in response to mm256

Personally, I did not like the idea of combining UNIX/POSIX scripts with AppleScript. To your point, . . . kind of messy. I implemented a similar algorithm as the AppleScript example in korn shell with the terminal app


#!/bin/ksh

today=`date '+%Y%m%d'`


delay=7

#

echo

echo Locate any data in the users' .trash directory or disk's .trashes directory. If previously scheduled to be removed, move it to a directory for mass removal. else schedule a date + 1 week it should be removed permanently.

echo



find /Volumes/*/.trashes/* /Volumes/*/.trashes/.[0-z]* /Users/*/.Trash/* -prune -exec ls -1d {} \; 2>/dev/null | grep -v trashInfo | grep -v deleteNow | while read trashFile

do

trashDir=`dirname "${trashFile}"`

base=`basename "${trashFile}"`

trashInfo="${trashDir}/.${base}.trashInfo"

echo -n "looking at ${trashFile}: "

du -sh "${trashFile}" | awk '{print $1}'

if [ -r "${trashInfo}" ]

then

dateToSRM=`awk '{print $NF}' "${trashInfo}"`

if [ $dateToSRM -lt $today ]

then

if [ ! -d "${trashDir}"/.deleteNow ]

then

mkdir -p "${trashDir}"/.deleteNow

fi

mv "${trashFile}" "${trashDir}/.deleteNow"

mv "${trashInfo}" "${trashDir}/.deleteNow"

fi

else

echo "date to secureRemove: " `date -v+${delay}d '+%Y%m%d'` > "${trashInfo}"

fi

done


echo

echo locate any trashinfo files without actual trash file and move it to the pending removal directory

echo


find /Volumes/*/.trashes/* /Volumes/*/.trashes/.[0-z]* /Users/*/.Trash/* -name '*.trashInfo' -prune -exec ls -1 {} \; 2>/dev/null | grep -v deleteNow | while read trashInfo

do

base=`basename "${trashInfo}" .trashInfo | sed s/^\.//`

trashDir=`dirname "${trashInfo}"`

if ! [ -r "${base}" ] > /dev/null 2>&1

then

mv "${trashInfo}" "${trashDir}/.deleteNow"

fi

done


echo

echo remove anything in the pending removal directory

echo


du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* | sort -nr | uniq| while read size realTrash

do

echo -n "looking at ${realTrash}: "

du -sh "${realTrash}" | awk '{print $1}'

chmod -R 777 "${realTrash}"

srm -rfmz "${realTrash}" 2>/dev/null &

done

numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

while [ $numFiles -gt 0 ]

do

echo

echo

du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* 2>&1 | grep -v "No such file" | sort -nr | head -5 | awk '{printf "%2.2f MB %s\n",$1/1024,$2}'

sleep 60

numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

done

Jul 16, 2013 5:47 PM in response to Linc Davis

Linc Davis wrote:


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.

To your point, trash is a safeguard against accidental deletion. Nothing should be put in Trash unless you intend to delete it. But ultimately by nature of a safeguard it does keep stuff temporarily. Think of it as a utility the same as paying a trash collector to come to your home and pick up your trash once a week.

Jul 16, 2013 6:00 PM in response to Frank Caggiano

Frank Caggiano wrote:


If that's your requirements then you should be sweeping your documents directory (or entire HD) and removing the files that are past the time you want to keep them. Moving a file into the trash and expecting it to be there a week later is foolish.

🙂

Not quite the point of the thread. There is some level of expectation when deleting a file, it should never be available anymore period. Just like taking out the trash at home. On Monday mornings, I know it is no longer available.

Jul 16, 2013 6:06 PM in response to Hawaiian Scuba Dude

Hawaiian Scuba Dude wrote:


numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

while [ $numFiles -gt 0 ]

do

echo

echo

du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* 2>&1 | grep -v "No such file" | sort -nr | head -5 | awk '{printf "%2.2f MB %s\n",$1/1024,$2}'

sleep 60

numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

done

This section is for monitoring the progress if there is a large amount of trash

Jul 16, 2013 7:26 PM in response to mm256

now (a year later) I could write a simpler script, but the central problem still remains: Neither applescript nor unix knows when a file was moved into the trash. even stat (which can tell when files were moved into any other folder) doesn't know that. If you want deletions based on the age of the file or the last modification date, fine, but to get the date the file was added to the trash is more complex.

Jul 17, 2013 1:37 AM in response to Hawaiian Scuba Dude

I found where was the problem.

Just executed:

osascript /Users/user1/Library/Scripts/dated_deleter.scpt

and got:

/Users/user1/Library/Scripts/dated_deleter.scpt: execution error: This script contains uncompiled changes and cannot be run. (-2700)

Сonclusion: compile script in AppleScript editor by pressing "Compile" after you change it and before saving.

Jul 17, 2013 11:20 AM in response to Tony T1

Tony T1 wrote:


Just use find and launchd (or cron):


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


N.B. From testing yesterday, it seems that (on 10.8.4) the -newerct option will not compare the date/time the item was moved into the trash, but instead uses the date/time that the item was moved into its previous folder. I don't know if that's a bug, a feature, or an omission, but test it out yourself. That can produce unexpected behavior: if the item was moved into its original folder a month ago it will be deleted immediately, because the system will think it's been in the trash for a month.


Of course, if that's the behavior your looking for, all's well. But it seems wrong to me.

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 Account.