Automatically Empty the CIFS/SMB Recycle Bin On A FreeNAS Server
Ever wanted to setup FreeNAS to empty the recycle bin automatically for you? Well, this should help!
First off, if you want to enable the recycle bin, go to the share you want to enable it for (Services -> CIFS/SMB -> Shares) and edit that share. About half way down, you will find a checkbox for "Enable recycle bin".
Second, we need to create a .sh file
(the following method seemed to give me the least amount of trouble...)
Go to Advanced -> Command
In the box, type:
mkdir /usr/opt/Hit the Execute button, then type:
touch /usr/opt/autorecycle.shHitting the Execute button to finish.
Next, go to Advanced -> File Editor
For the path, either type:
/usr/opt/autorecycle.shthen click the load button.
OR
Click on the "..." button to navigate to the autorecycle.sh file.
Copy and paste the following into the blank box:
#!/bin/sh
find /mnt/tank0/.recycle/* -atime +14 -exec rm -rf '{}' \;
find /mnt/tank0/.recycle/ -depth -type d -empty -exec rmdir {} \;Notice the /mnt/tank0/.recycle is the path to my recycle bin. You will need to change this to reflect your recycle bin path.
Notice the "+14". That's how old a file has to be before it deletes it from the recycle bin. If you want 7 days, change it to +7. If you want 30 days, change it to +30
Finally, go to: System -> Advanced -> Cron and create a new cron job, setting the following:
Command: sh /usr/opt/autorecycle.sh
Who: root
Description: Empty recycle bin of files older than 14 days
Schedule time:
- Minutes: Selected | 0
- Hours: Selected | 0
- Days: All
- Months: All
- Week days: All
(The scheduled time can be set for whenever you wish the cron job to happen... As shown above, I set it for Midnight)
Save it and you're done! If you want to manually run the cron job, edit the job and click the "Run Now" button. This is also another way of testing to make sure everything is setup right, otherwise you will get a "Cron Job Failed to Execute" error.
Update 2011/03/04: Updated the script to delete empty directories by adding: find /mnt/DATA/.recycle/ -depth -type d -empty -exec rmdir {} \; which was suggested by Alan in the comments below. Thanks Alan for the updated script!
The Wichita Computer Guy is hosted by one of the best hosting companies on the market: Hostgator.com. Not only do they provide unlimited domain addons, subdomains and parked domains, they also offer unlimited databases, unlimited hosting space and unlimited bandwidth! We have been using Hostgator for several years now and had great customer service support and almost 100% uptime. If you are looking for a website hosting company that provides everything you need for a powerful CMS based website, take a look at Hostgator.com!!

A scropt to clean out all files older than 30 days from shares
I like the script example you provided thanks! Could you set this script to run for a share and have it clear out files older than 30 days? Sorry for my newbie question..
find /mnt/tank0/.recycle/* -atime +14 -exec rm -rf '{}' \;
example:
find /mnt/MGT/DS/* -atime +30 -exec rm -rf '{}' \;
I run this manually and it fails :-(
Doesn't delete empty directories
Apparently the "find" changes the atime on the directories so they don't get old enough to delete.
See http://sourceforge.net/apps/phpbb/freenas/viewtopic.php?f=46&t=6224&star...
Any ideas on how to get rid of the empty directories?
Solved
http://duramecho.com/ComputerPrograms/DeleteEmptyDirectories/index.html
find /mnt/path -depth -type d -empty -exec rmdir {} \;
I'm just adding that to my autorecycle script.
By the way, I keep my scripts on the data drive so that I don't have to restore anything when I upgrade or reinstall -- just restore the config and everything works!
The cron script just points to /mnt/b/autorecycle.sh.
All my mounted drives are in directories below that level so nobody sees those files anyway.
Rockstar!
Nice! Whenever I get around to replacing the PSU in my Freenas server, I'll do some playing around with that line and update the tutorial :)
Thx for the fix!
Summary/recap
Summary/recap of all the above posted at:
http://sourceforge.net/apps/phpbb/freenas/viewtopic.php?f=46&t=8755
Hum...
At this point, I don't have any suggestions... Apparently I've created a bit of a mess, looking at the forum you posted, BUT, I'm glad to know I wasn't the only person who wanted an automatic purge of the recycle bin... Maybe this will prompt the dev's to include it an option in future releases :)
The only thing I can point to, is the ctime that someone mentioned in the forum... Since I'm not a nix guru, I can't provide a whole lot of helpful info in troubleshooting until I have a chance to get my hands dirty and at that point, I'll update the tutorial. I have a feeling that I haven't checked for empty folders to know that was a problem. I'll see what I can do to look into it.
By the way, the discussion on
By the way, the discussion on the forum was about the difference between the syntax for the command-line vs. a shell script. Your syntax is correct and works fine on the latest version of FreeNAS (7.2).
Thanks - I'm still using it
Thanks - I'm still using it as its better than letting all those deleted files sit there. If I do find a solution I'll post a note here.
It would be nice if FreeNAS would start deleting the oldest files when it needed the space (like Novell File Systems do on Netware or their OpenServer).
Freenas
Nice!
it works great.
thank you
Thank you!
Great tip, thank you sir!