Empty recycle bin

Status
Not open for further replies.

Wolfeman0101

Patron
Joined
Jun 14, 2012
Messages
428
I have the recycle option on and I was wondering if there is a way to empty the recycle bin as files reach a certain date.

For example I want all files I deleted over 7 days ago to purge.

Is this possible?
 

califrag

Dabbler
Joined
Sep 9, 2011
Messages
16
I'm not sure where I got this, but create a script, i named mine 'autorecycle.sh', (make sure to chmod +x it), with these contents:

Code:
#!/bin/sh

find /mnt/tank/.recycle/* -atime +30 -exec rm -rf '{}' \;
find /mnt/tank/.recycle/ -depth -type d -empty -exec rmdir {} \;



replace 'tank' with whatever your mount is, it will remove empty directories and files and folders over 30 days (change 30 to 7 to remove those over 7 days)

You'll need to put this somewhere (i put mine in /usr/opt/ - had to create the opt folder) and add it as a cron job through the web gui.

Code:
sh /usr/opt/autorecycle.sh


Run it once a week if you wanna do 7 days or once a month for 30 days.

Hrm... I think there is a forum post around here I'll try to find it.

Cant remember which one I got this from:
http://thewichitacomputerguy.com/blog/automatically-empty-cifssmb-recycle-bin-freenas-server
http://sourceforge.net/apps/phpbb/freenas/viewtopic.php?f=46&t=8755

I think the first one.

keep in mind if you put it in /usr/opt/ like me, it will get overwritten after an update.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I hate people that necro threads, but I feel I should point this out.. I'm guessing someone actually has the solution as I don't (but I'm looking for it and I'll post back when I have a working script).

Lets say you have the following locations with the following shares:

/mnt/tank/Movies = Movies
/mnt/tank/Music = Music
/mnt/tank/Stuff = Stuff
/mnt/tank/Documents = Documents

If you delete something from the Movies share, it will end up in /mnt/tank/Movies/.recycle.
If you delete something from the Music share, it will end up in /mnt/tank/Music/.recycle.
If you delete something from the Stuff share, it will end up in /mnt/tank/Stuff/.recycle.
If you delete something from the Documents share, it will end up in /mnt/tank/Documents/.recycle.

You will need to be proactive with adding these lines for each share you ever create, else your recycle bin for that share will only continue to grow. If someone has an idea for how to modify the script to actually seek out folders that are named .recycle and then apply the following commands that would be very cool and very helpful for myself. I'm sure many other people that like to set-it-and-forget-it would love a script that seeks out the .recycle folders automatically. Additionally, if there was a way to delete an empty .recycle folder itself that would also be helpful.

Yes, I'm a little OCD about my files being organized and not letting stray orphans collect on my servers. :)
 

Wolfeman0101

Patron
Joined
Jun 14, 2012
Messages
428
I hate people that necro threads, but I feel I should point this out.. I'm guessing someone actually has the solution as I don't (but I'm looking for it and I'll post back when I have a working script).

Lets say you have the following locations with the following shares:

/mnt/tank/Movies = Movies
/mnt/tank/Music = Music
/mnt/tank/Stuff = Stuff
/mnt/tank/Documents = Documents

If you delete something from the Movies share, it will end up in /mnt/tank/Movies/.recycle.
If you delete something from the Music share, it will end up in /mnt/tank/Music/.recycle.
If you delete something from the Stuff share, it will end up in /mnt/tank/Stuff/.recycle.
If you delete something from the Documents share, it will end up in /mnt/tank/Documents/.recycle.

You will need to be proactive with adding these lines for each share you ever create, else your recycle bin for that share will only continue to grow. If someone has an idea for how to modify the script to actually seek out folders that are named .recycle and then apply the following commands that would be very cool and very helpful for myself. I'm sure many other people that like to set-it-and-forget-it would love a script that seeks out the .recycle folders automatically. Additionally, if there was a way to delete an empty .recycle folder itself that would also be helpful.

Yes, I'm a little OCD about my files being organized and not letting stray orphans collect on my servers. :)
Your not correct there.

They will go into:
/mnt/tank/.recycle/[user]/Movies
/mnt/tank/.recycle/[user]/Music
/mnt/tank/.recycle/[user]/Stuff
etc.

The script will delete the folders as they empty.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I just looked and I have /mnt/tank/.recycle, /mnt/tank/Entertainment/.recycle, and /mnt/tank/Backups/.recycle so I disagree unless they just changed it in 8.3.1.
 

matejz

Cadet
Joined
Jun 29, 2016
Messages
1
I apologize for the necro, but I'm sure people are stumbling across this thread just as I did and I think I can contribute some useful knowledge.

The "search and destroy" functionality - searching out the recycle bin and purging it - is pretty straightforward and can be done with a wildcard.
Code:
find /mnt/tank/*/.recycle/* -atime +30 -delete

This command will find any folder named .recycle anywhere in the "tank" dataset regardless of share and delete everything in it older than 30 days. The threshold is determined by the atime flag and can be changed to your desires. You may also note that instead of running rm -rf everywhere, I'm using the -delete flag. Both will work, but having a script going around running rm -rf makes me a little uneasy, so I prefer not to do it. Before running this for the first time, I would also recommend opening up ssh and running the command without the -delete flag; the output should be a list of files that meet the criteria for deletion and looking over these now could save you a ton of snapshot rollbacks later. Note that if you're getting an error about "no such directory," it's likely because the .recycle directory is deeper in the directory structure; "*" will only expand to a single level of the structure, so in the example above we're looking inside every folder in "tank" for a folder named ".recycle" and if ".recycle" happens to be in a subfolder, find won't see it.

The second command doesn't need much tweaking, but I would again use the -delete flag instead of rm -rf. Additionally, removing the -depth flag will make it also evaluate the .recycle directory itself and remove that if it's empty as well.

My final note is that a script for two lines just isn't necessary. All *nix operating systems can fit as many commands as you want on a single line and, while not practical for longer functions, with just two commands it's worth it in order to avoid having to save the script somewhere and having to use something other than the freenas web ui to edit it. By simply putting
Code:
find /mnt/tank/*/.recycle/* -atime +30 -delete; find /mnt/*/.recycle/ -type d -empty -delete

in the command field of the cron job, we can find all recycle bins in all shares, delete any contents over 30 days old, remove all empty directories within the recycle bin, and then remove the .recycle folder itself if there's nothing in it.
 
Last edited:
Status
Not open for further replies.
Top