Script to clean up old Trash, Junk and Spam mail.

Discussion in 'Tips/Tricks/Mods' started by EckyBrazz, Aug 17, 2012.

  1. EckyBrazz

    EckyBrazz New Member

    Hi Folks,

    Just found a script that I have adjusted to work with ISPConfig 3 to clean up the old trash mail ect.

    MAILDIRS=$(find /var/vmail/*/*/Maildir/ -maxdepth 0 -type d)
    for basedir in $MAILDIRS; do
      for dir in .Trash .Junk .Spam .Low\ Priority; do
        for dir2 in cur new; do
          [ -e "$basedir/$dir/$dir2" ] && (
            echo "Processing $basedir/$dir/$dir2..."
            find "$basedir/$dir/$dir2/" -type f -mtime +30 -delete
    When migration from Cpanel to ISPconfig I found that my users have a lot of old mails in the trash. When I ran this script it saved me about 25GB of diskspace.
  2. Member

    super ... just what I was looking for. I was having hard time and thought of writing one utility ...

    :) Thanks mate
  3. andypl

    andypl Member

    Scripts not working

    find: `/var/vmail/*/*/Maildir/': No such file or directory
  4. danhansen@denmark

    [email protected] Member HowtoForge Supporter

    Great idea - Scripts not working yet ...


    Sound really great!
    This is exactly what's always missing, in any case of software making.. Order, clean-up functions etc. - any chance of getting it to work on ubuntu server 10.04/ispconfig 3 ?

    Looking forward to hear from you guys :)

    Kind Regards.
    [email protected]
  5. Bashewa

    Bashewa Member

    You can run this either from command line or via a cron

    find /var/vmail/*/*/.Junk/new/ -mtime +30 -exec rm {} \;

    Above will delete any file older than 30 days from all .Junk/new/ folders


    The first argument is the path to the files. This can be a path, a directory, or a wildcard ( For all email accounts ) as in the example above. I would recommend that you run the command without the exec rm to make sure you are getting the right results.
    Only use rm as this will not remove any directory only files

    The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +30, it will find files older than 30 days.

    The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
    Th0m likes this.
  6. mitsos

    mitsos New Member

    I'm personally using:
    find /var/vmail/*/*/Maildir/.Trash/new/ -mtime +30 -type f -delete
    find /var/vmail/*/*/Maildir/.Junk/new/ -mtime +90 -type f -delete
    find /var/vmail/*/*/Maildir/.Junk\ E-mail/new/ -mtime +90 -type f -delete
    which is a file created in /etc/cron.daily/ and chmoded to 755

    Works perfectly for me. Trash older than a month gets deleted, spam older than 3 months gets deleted.
    Last edited: Apr 9, 2014
  7. Turbanator

    Turbanator Member HowtoForge Supporter

    Here's another that we use and looks at timeframe to delete as well.

    # Time to wait before removing mails from the Junk folder (Default: 7 days) Set 0 to turn off.
    # Time to wait before removing mails from the Trash folder (Default: 30 days) Set 0 to turn off.
    for domain in /var/vmail/*
      if [ -d "$domain" ]
        for user in $domain/*
          if [ "$junk_max_hours" -gt "0" ]
            if [ -d "$user/.Junk" ]
              tmpreaper -m $junk_max_hours $user/.Junk/cur
             tmpreaper -m $junk_max_hours $user/.Junk/new
          if [ "$trash_max_hours" -gt "0" ]
            if [ -d "$user/.Trash" ]
              tmpreaper -m $trash_max_hours $user/.Trash/cur
            tmpreaper -m $trash_max_hours $user/.Trash/new
  8. Musarrat Hussain

    Musarrat Hussain New Member

    I want to use this command in sendmail for pruning Trash Folder, instead of using as a script i want to use / test as a single command for single user.
    Please help...
  9. Jesse Norell

    Jesse Norell Well-Known Member

    Not sure what you mean, that script is simply 3 commands, you can run them from the command line manually rather than together as a script. For a single user, just change the path to that mailbox, eg.
    find /var/vmail/ -mtime +30 -type f -delete
    If you use dovecot it's even easier and should also update the maildir quota:
    doveadm expunge -u [email protected] mailbox Trash savedbefore 30d
    If you have dovecot 2, you can run that for all mailboxes with:
    doveadm expunge -A mailbox Trash savedbefore 30d
    If that returns 'Error: User listing returned failure', you need to enable iterate_query:
    sed -i 's/^# iterate_query/iterate_query/' /etc/dovecot/dovecot-sql.conf
    (change that in a conf-custom file to make it persist ispconfig updates).
  10. florian030

    florian030 ISPConfig Developer ISPConfig Developer

    Or use the expire or autoexpunge-plugin with dovecot
  11. Musarrat Hussain

    Musarrat Hussain New Member

    Thanks Jesse & Florian,
    I am a newbie in Linux specially for MTA, and some of my questions may seems stupid and i am sorry in advance...
    I can't find below locations
    find /var/vmail/
    what i found in my server is
    find /home/mail/username/mail/
    and there i can't able to use option of -mtime ...
    Similarly doveadm resturns command not found though dovecot is installed and running, version is 1.0.7
    how to use expire or autoexpunge plugin?
    Sorry once again if it sounds stupid.
  12. Jesse Norell

    Jesse Norell Well-Known Member

    /var/vmail/ is the place where ispconfig will store email by default; I believe you can change that, but you would likely know if you had done that. '' is just an example domain name, and 'email1' is an example email address - you need to change those to the domain name and address which you are wanting to to clean up (ie. the 'single user' you referred to).

    This is not anything ispconfig would use by default, eg. if you followed one of the Perfect Server setup guides here on howtoforge, so might well be unused. Look at /var/vmail/ and see what's under there, eg. do you have a directory for each of your domains? Does /home/mail/ have a directory for all your email addresses, or just one particular user you see there?

    Are you logged in as a normal user, or root? If a normal user, either switch to root, or use sudo (eg. 'sudo doveadm expunge -u [email protected] mailbox Trash savedbefore 30d'). If root, what linux distribution/version are you running?

    The expire plugin is to optimize disk I/O when doing dovadm expunge -A, it's not used at all for individual users (-u). From the documentation:
    autoexpunge would allow you to configure a similar cleanup to be done automatically, though I don't know if you can limit it to a single user (maybe?), and it isn't available in dovecot 1.
  13. Musarrat Hussain

    Musarrat Hussain New Member

    Dear Jesse,
    thanks for the details you have shared. but still confusing for me as i do not have dir /var/vmail instead i am seeing a dir /var/mail and this directory contains all the accounts e.g. my account m.hussain but its a file and nothing in it...
    yes, i logged on with root account but still getting -bash: doveadm: command not found
    i m using CentOS release 5.9 (final)

Share This Page