cPanel clear all default email accounts with a cron

There is a mobile optimized version of this page, view AMP Version.

One of my biggest annoyances when it comes to cPanel/WHM is the fact that every cPanel account has a catch-all mail account, referred to as a "default" system account - it is extremely useful if you want any email address for a domain to route email into an inbox that can be picked up, but more often you would setup a forwarder so that those emails go to a real email address that you can access with your preferred mail software.

This is where things get annoying, you can setup a forwarder, but, emails still get cached and stored in the default mail account, so this account continues to keep building up forever even though you are getting and cleaning out unwanted emails from your main mail account.

Even worse is that sometimes you may have this enabled by default for your clients who are unaware and not even using the feature, so unwanted, spam emails are just building up in an invisible inbox forever...

Well I found a neat script that can be setup on a cron that will clear all cPanel accounts of their "default" inbox periodically, in this example I will be running mine once per day, but you can modify your cron conditions to run at any interval you desire.


The Commands (Manual SSH)

If you just want the commands to run all you need to do is run the two following commands as root user through your SSH:

find /home/*/mail/new/ -type f -delete
find /home/*/mail/cur/ -type f -delete


CronTab (Automated)

If you would like the cleanup to run automatically at set intervals open up your SSH and type:

crontab -e

enter a new line for each of the cron instructions, something like this:

12 2 * * * find /home/*/mail/new/ -type f -delete
12 2 * * * find /home/*/mail/cur/ -type f -delete

This will run every night ar 2 minutes past 12.

Once saved you will need to restart the daemon:

service crond restart

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips