Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > ISPConfig 3 > Tips/Tricks/Mods

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Thread Tools Display Modes
Old 17th August 2012, 19:00
EckyBrazz EckyBrazz is offline
Join Date: Jul 2012
Location: Goi‚nia, Brazil
Posts: 37
Thanks: 3
Thanked 4 Times in 1 Post
Default Script to clean up old Trash, Junk and Spam mail.

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.
Michael Burgos
Reply With Quote
The Following 4 Users Say Thank You to EckyBrazz For This Useful Post:
falko (18th August 2012), florix.net (21st March 2013), technoid (24th March 2013), till (18th August 2012)
Sponsored Links
Old 21st March 2013, 17:36
florix.net florix.net is offline
Join Date: Oct 2010
Posts: 42
Thanks: 4
Thanked 1 Time in 1 Post

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

Thanks mate
Reply With Quote
Old 22nd March 2013, 13:34
andypl andypl is offline
Senior Member
Join Date: Mar 2006
Location: Warsaw, Poland
Posts: 100
Thanks: 1
Thanked 6 Times in 4 Posts

Scripts not working

find: `/var/vmail/*/*/Maildir/': No such file or directory
Reply With Quote
Old 22nd March 2013, 21:24
danhansen@denmark danhansen@denmark is offline
Senior Member
Join Date: Mar 2013
Posts: 255
Thanks: 55
Thanked 4 Times in 4 Posts
Post 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.
Reply With Quote
Old 16th September 2013, 03:56
Bashewa Bashewa is offline
Junior Member
Join Date: Feb 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts

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.
Reply With Quote
Old 9th April 2014, 18:34
mitsos mitsos is offline
Junior Member
Join Date: Mar 2014
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post

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 by mitsos; 9th April 2014 at 18:41. Reason: forgot to add the chmod
Reply With Quote
Old 15th April 2014, 00:03
Turbanator Turbanator is offline
Senior Member
Join Date: Jun 2008
Posts: 220
Thanks: 23
Thanked 16 Times in 16 Posts

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
Reply With Quote
The Following User Says Thank You to Turbanator For This Useful Post:
srijan (15th April 2014)


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Move Spam Emails to Junk directory by default? Spamfilter = Normal by default? radim_h Tips/Tricks/Mods 4 18th January 2012 00:25
Junk mail folder Rescue9 General 2 23rd March 2010 15:00
imapd: Error reading ACLs for INBOX..Junk: Invalid argument playalistic Installation/Configuration 6 2nd December 2009 12:42
Move junk mail to a folder instead of deleting grant Tips/Tricks/Mods 20 23rd October 2008 00:09
Junk mail and spamassassin... sthompson Installation/Configuration 4 27th December 2006 17:11

All times are GMT +2. The time now is 14:52.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.