Automatically Empty the CIFS/SMB Recycle Bin On A FreeNAS Server

Author:
phil
Created:
Wednesday, October 21st, 2009
Last Updated:
Saturday, February 16th, 2019

Disclaimer: Accessing the information on this page means you agree to the Sites Terms of Service


Update 2019/02/16: This page is pretty well outdated... As suggested by Alan in the comments below, head over to: http://blog.hani-ibrahim.de/en/automatic-purge-samba-recycle-bin.html and check out an alternative or updated version of this method.

---

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

Hitting the Execute button to finish.

Next, go to Advanced -> File Editor
For the path, either type:

/usr/opt/autorecycle.sh

then 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!

Post Comment

Comments

thank you very much !!!!
It worked very well nas4free :D
regards

It is obvious to someone who knows but for those who don't... Maybe you should say to save the created file before going to put in the cron entry.

The below script was originally written for Ubuntu Server 12.04 LTS running Samba.
This script will log all removed objects and remove; a) files older than 7 days, b) empty directories and c) empty files.
Use the same method as described to automate the house cleaning (using Cron).

#!/bin/bash

###
#  meant to cleaup deleted/recycled files & folders
#  they will have been kept for a while, probably 7 days
#  then cleared out with this script, run from cron/anacron
###

# Create Temp Log File
RUNON=$(date +"%F__%R")
TMPLOGFILE=$(mktemp "houseCleaning_$RUNON.XXXXXXXXX" --tmpdir=$TMPDIR)
LOGDIR=/var/log
LOGFILE=$LOGDIR/houseCleaning.log

# Find and Log files older than 7 days old
echo "finding files deleted more than 7 days ago" >> $TMPLOGFILE
find /mnt/vol0/*/.recycle/* -type f -mtime +7 >> $TMPLOGFILE
# Delete files older than 7 days
find /mnt/vol0/*/.recycle/* -type f -mtime +7 -delete

# Find and Log empty directories
echo "finding empty directories" >> $TMPLOGFILE
find /mnt/vol0/*/.recycle/* -type d -empty >> $TMPLOGFILE
# Delete empty directories
find /mnt/vol0/*/.recycle/* -type d -empty -delete

# Find and Log empty files
echo "finding empty files" >> $TMPLOGFILE
find /mnt/vol0/*/.recycle/* -type f -empty >> $TMPLOGFILE
# Delete empty files
find /mnt/vol0/*/.recycle/* -type f -empty -delete

# Update Log File
echo "Script:   $0" >> $LOGFILE
echo "Executed: $RUNON" >> $LOGFILE
cat $TMPLOGFILE >> $LOGFILE
echo "------------------" >> $LOGFILE

I stumbled across this thread while looking for a way to empty the .recycle folder. Not a Linux person by any means, but your description was easy to follow and I didn't end up wrecking anything.

Always happy not to wreck things...

I also found that you can set this script to run at post-init (or pre or at shutdown) by adding it into the System --> Advanced --> command Scripts menu. I don't generally have my NAS box on 24/7, so the Chron didn't feel right.

Freed up 200Gig of space, was nearly out of room on the share - so glad you posted this for nubbie like me!

Many thanks!!

I'm an idiot... I didn't see the "sh" before the filename... that probably allows execution of a script indirectly, eh? haha

I had to run

chmod a+x /usr/opt/autorecycle.sh

in order for the script to work... thought I'd share in case anyone else ran into issues. Very useful script!

I'm new to freenas and I just installed version 8. I can't find the "Advanced -> Command" where is it in the latest version?

thank you

Well, I'll be honest... I haven't had much experience with Freenas 8. I installed it and played around with it, but found it a little more difficult to set things up when compared to v7. I love the new interface of 8, but I felt like the overall project needs some work. So, with that being said, I haven't played with it enough to know where you can enter the command.

For some reason, I was thinking I saw a setting that would empty the trash. Something that built into the interface. (I could be totally wrong though...)

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 :-(

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?

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.

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.

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!

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

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

The above link is now dead, since the now-FreeNAS project deleted the original FreeNAS project on SourceForge (original FreeNAS became NAS4Free, now XigmaNAS ®).

Here's a link to a better summary for automatically purging the SAMBA Share recycle bin: http://blog.hani-ibrahim.de/en/automatic-purge-samba-recycle-bin.html

Thanks for the update @Alan. I've updated the OP with a link over to that page. I haven't used FreeNAS in years so I can't really help with it any more.

Nice!
it works great.
thank you

Great tip, thank you sir!