Automatically cleaning Kerio Mail Folders


Some users just won't clean up their mail folders. They may delete stuff from their Inbox, but they'll never touch their Junk Mail folders or the deleted items. You can set quotas, but that's annoying, and if the offending party is a boss, you mind find yourself facing salary quotas or worse.

If you are running Kerio on a Linux or Mac OS X server, there is a solution: a shell script run with cron (or Launchd for Mac OS X) can automatically clean up whatever you like. Here's the basic script:

test "$dir" || exit 1
cd "/opt/kerio/mailserver/store/mail/$user/$dir/#msgs" || exit 0
find . -mtime +1 -exec rm -f {} \;
cd "/opt/kerio/mailserver/store/mail/$user/$dir/" || exit 0
/bin/mv index.fld index.bad

You'd call that with the users and directories you want to clean. For example:

/etc/init.d/keriomailserver stop
cleankerio tony "Junk Mail"
cleankerio tony "Deleted"
cleankerio same "Deleted"
/etc/init.d/keriomailserver start

You need to adjust this script to reflect the proper directory for your system: Mac is different than Linux and both allow you to put the "store" directory wherever you like. You'd run this with a cron job, probably after working hours to avoid complaints from users.

A couple of things: stopping the mail server in the calling script is absolutely necessary, as is renaming the "index.fld" files in the working script. The "cd .. || exit 0" is important to guard against accidentally removing the wrong files. You can adjust the -mtime argument to "find" if you want to leave files in the directories longer.

By the way, this same technique can be used to add messages to a user's mailboxes without going through the smtp process. You do have to be concerned about overwriting existing emails, but basically you just stop the server, copy the messages in, rename the "index.fld" file, and restart the server.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Automatically cleaning Kerio Mail Folders


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Wed Jun 18 19:14:31 2008: 4345   mvgfr

Cool script; thanks.

Are you certain stopping the server is necessary?

The docs seem to indicate that in some cases it's not - for instance, if you can ensure the user is not logged on, you can use the "index.bad" method of resetting that user's indices.

- Marc

Wed Jun 18 19:19:54 2008: 4346   TonyLawrence

Right.. but since we can't know when someone will try to connect, it's safer to just stop - unless you are just fixing one user and you are certain they are out.

Wed Jun 18 23:39:17 2008: 4347   mvgfr

OK; so I'm not crazy. :)

So, and I ask this in all seriousness, how do you stop the server - politically? Isn't email a critical enough service that virtually any outage, any time of day, is undesirable? Doesn't stopping the server that way have the potential to leave some user high and dry, with an unsaved change?

As to making sure a user is not connected, can't this be easily accomplished by: a) checking to see that they're really not connected via Kerio admin (not sure if there's a way via the command line), and b) for extra safety if desired, temporarily disabling the account, perhaps by something interesting like switching the password "pointer" in OD?

- Marc

Wed Jun 18 23:49:51 2008: 4348   TonyLawrence

Well, users can retry, but we run this at a time of day when it's unlikely for anyone to be on.

If users would clean up their own junk, we wouldn't need things like this.

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

privacy policy