Automatic Cleaning Of Trash And/Or Junk Folders With ISPConfig (With Roundcube + Tmpreaper (Tmpwatch))


This short mini-howto will help you set up automatic cleaning of Trash- and Junkfolders. Most (web)mail clients (can) automatically create these. A lot of people don't clean there maildirectories causing Junk and Trash folders to grow massively in size and with ISPConfig (not yet) being able to set good maildirectory size limits the alternative is to have these folders cleaned after several days.



This tutorial assumes you are currently using the following or similar configuration:

  • Linux Debian Etch (v4.0)
  • ISPConfig (v2.2.24 or later)
  • Make  
  • Nano or vi
  • Roundcubemail (v0.1.1 package install)
  • Atleast 1 domain with 1 mailbox running
  • Junk mail set to be moved to a folder instead of deleting (If not set, See links)

If this configuration does not match yours, you might need to adjust the howto to fit your configuration.


Tutorial move junk mail to a folder instead of deleting:
Roundcubemail v0.1.1 package install: 


1 - Set roundcubemail to automatically create Trash and Junk folders

If you have already done this either by Roundcubemail or other methods, you can skip this chapter. Make sure .Trash and .Junk exist.

To set Roundcubemail to automatically create mailfolders we need to modify the Roundcubemail configuration:

nano /home/admispconfig/ispconfig/web/roundcubemail/config/

Locate "$rcmail_config['create_default_folders']" and make sure it is set to TRUE and make sure the configuration is similar to:

// store draft message is this mailbox
// leave blank if draft messages should not be stored
$rcmail_config['drafts_mbox'] = 'Drafts';
// store spam messages in this mailbox
$rcmail_config['junk_mbox'] = 'Junk';
// store sent message is this mailbox
// leave blank if sent messages should not be stored
$rcmail_config['sent_mbox'] = 'Sent';
// move messages to this folder when deleting them
// leave blank if they should be deleted directly
$rcmail_config['trash_mbox'] = 'Trash';
// display these folders separately in the mailbox list.
// these folders will also be displayed with localized names
$rcmail_config['default_imap_folders'] = array('INBOX', 'Drafts', 'Sent', 'Junk', 'Trash');
// automatically create the above listed default folders on login
$rcmail_config['create_default_folders'] = TRUE;
// protect the default folders from renames, deletes, and subscription changes
$rcmail_config['protect_default_folders'] = TRUE;

Now when an user logs in the mailfolders will be created.

NOTE: This requires that the user needs to have logged in, else trash and junk folders just do not exist and won't be cleaned ofcourse!


2 - Install tmpreaper

The package tmpwatch used to be in the package manager Aptitude however for unknown reasons it is been replaced. For other linux variants, see tmpwatch.

To do this we enter the following command:

apt-get install tempreaper

IMPORTANT: If you can use tmpreaper skip to chapter 3. If you you have to use tmpwatch don't forget to modify the cron file!

If you cannot use tmpreaper you can download & install tmpwatch as followed:

cd /usr/src
tar xvzf tmpwatch-2.9.0.tar.gz
cd tmpwatch-2.9.0
make install

Thanks to MTvermoes!


3 - Add a cronjob

We now need to add a cronjob so the system will check daily which emails are going to be needed for deletion. We suggest you set this to cron.daily making it check every day for mails that are old enough to be deleted. However you could also place it in cron.hourly which is neccesary if you use a maximum time of less than 24 hours else it still would only be checked on daily basis.

To do this we go to /etc/cron.daily

cd /etc/cron.daily

Now we create a new file "clean-mailfolders"

nano clean-mailfolders

And make it look like:

# 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/www/*
  if [ -d "$domain"/user ]
    for user in $domain/user/*
      if [ "$junk_max_hours" -gt "0" ]
        if [ -d "$user/Maildir/.Junk" ]
          tmpreaper -m $junk_max_hours $user/Maildir/.Junk/{cur,new}
      if [ "$trash_max_hours" -gt "0" ]
        if [ -d "$user/Maildir/.Trash" ]
          tmpreaper -m $trash_max_hours $user/Maildir/.Trash/{cur,new}

You might want to change the dates to fit your own needs. In the beginning of the file 2 variables are given named "junk_max_hours" containing the maximum of hours mail in the Junk folder is kept and "trash_max_hours" containing the maximum of hours mail in the Trash folder is kept. Setting the value to 0 will disable the cleanup of that type.

NOTE: If you have to use tmpwatch instead of tmpreaper don't forget to modify the cron file and replace "tmpreaper" with "tmpwatch".



Make sure that you check this before actually running it on production servers. This tutorials comes with no garantuee whatsoever. Be wise, test!

You can modify the variables to fit your own needs as described earlier.

Hope you like my first tutorial!

Share this page:

5 Comment(s)