Samba-Papierkorb

Automatic purge of Samba’s recycle bin on FreeNAS/NAS4Free

Samba can provide a recycle bin on a network share, as current operating systems do locally. Even FreeNAS and NAS4Free offers this feature and can be activated in its administration under Services|CIFS/SMB|Shares.

All deleted files and folders of a share are moved to this bin, physically localized on the network share. In comparison to a Windows’ recycle bin the maximum size is not settable. Therefore the recycle bin is able to overflow the share.

A cronjob and a tiny shell script can prevent this automatically.

In the file system of the share in FreeNAS/NAS4free the recycle bin is represented by the hidden folder .recycle. All deleted files and folder of a share of all users are stored there. The following method finally erases all files and folders of an appropriate age in this folder.

  1. Create a new file, e.g. delrecycle.sh
  2. Enter the following content to delrecycle.sh:
    #!/bin/sh 
    find /mnt/[MountPoint]/.recycle/* -atime +[Age] -exec rm -rf '{}' \;
    find /mnt/[MountPoint]/.recycle/ -depth -type d -empty -exec rmdir {} \;

    Adjust [MountPoint] (mount point of your share) und [Age] (age of the files in days which you want to erase) to your environment and needs.

    Update 07/12/2015: As “gilada” posted in a German comment that the script does not work correctly.  The error find: missing argument to `-exec' appeared. To get rid of this the semicolon (;) at the end of each line has to be masked by a backslash (\) in current UNIX versions. In my older FreeNAS-Version this was not necessary. I modified all scripts on this page. Thanks to “gilada” to point me to this issue.

    Example: If the name of your mount point is myshare and you want to finally erase all files and folders which were deleted for 90 days or more, the script  delrecycle.sh looks like:

    #!/bin/sh
    find /mnt/myshare/.recycle/* -atime +90 -exec rm -rf '{}' \;
    find /mnt/myshare/.recycle/ -depth -type d -empty -exec rmdir {} \;

    If you have more than one share you have to extend the script above. If your second mount point is called myshare2, the script is:

    #!/bin/sh
    find /mnt/myshare/.recycle/* -atime +90 -exec rm -rf '{}' \;
    find /mnt/myshare/.recycle/ -depth -type d -empty -exec rmdir {} \;
    find /mnt/myshare2/.recycle/* -atime +90 -exec rm -rf '{}' \;
    find /mnt/myshare2/.recycle/ -depth -type d -empty -exec rmdir {} \;
  3. Upload delrecycle.sh to your share.
  4. Create a cronjob (System|Advanced|Cron) with root– privileges, which calls the command:
    sh /path/to/MountPoint/delrecycle.sh

    daily. Following the example above, the command should look like:

    sh /mnt/myshare/delrecycle.sh
    Cronjob in FreeNAS

    Cronjob in FreeNAS

    In the screenshoot above the script is called at every first and fifteenth day of a month at midnight.

Peculiarities of Samba’s recycle bin

Because Samba’s recycle bin behaves different to the Desktop ones on Windows, Mac or Linux the article Peculiarities of Samba’s recycle bin may help.

Leave a Reply to Matze Cancel reply

Your email address will not be published.