Skip to main content.
December 3rd, 2013

cleanup the email buildup in cpanel

Ive had this ongoing problem with emails on hosted domains that use linux cpanel.

I’ve had this issue for over 8 years, and across 3 different hosting companies, so it must be quite widespread, yet nobody seems to be interested in fixing it.

So what’s the problem?

While using a pop3 email account, the hosting server accumulates email files that get downloaded to the email client.

Even if the email client is configured to delete the emails from the server after download, the cpanel server still keeps these emails… indefinitely!

In the early days, I would routinely get warnings about my disk storage reaching maximum capacity (ie I was running out of space).

After some investigation, I found that emails were the culprit, and there was nothing in place to fix it, other than to login to the cpanel, and manually remove the large number of files.

I managed to create my own solution, using my unix knowledge:

I created a cron job, which would run daily, which would scan all the mail folders, and delete any files that were older than, say, 60 days.

So as long as I checked my mail at least once every 60 days, then my system wouldn’t delete any undelivered emails.

Since starting my own hosting company, I have found this technique very useful in preventing my own customers from having similar problems.

So exactly what do I do?

If you are familiar with cron and unix scripts, the command looks something like:

13 3 * * * find ~/mail -name "*.*.*.*.*" -type f -mtime +60 -exec rm '{}' \;

In simple terms, what this means is:

at 3:13 in the morning, every day, execute the command:

find ~/mail -name "*.*.*.*.*" -type f -mtime +60 -exec rm '{}' \;

And the command means: search the folders under the mail folder, and find a file that has 4 dots in the name (*.*.*.*.*)

the -type f makes sure it only finds files, and not folders, or other weird stuff.

the -mtime +60 means make sure the files have been modified over 60 days ago

the -exec rm ‘{}’ \; means use the rm command (ie delete) on each file found ( the ‘{}’ will substitute the file name), and the \; means this is the end of the -exec part on the command.

Try this at your own risk… this has the potential to delete your whole website, so be extra careful, and don’t just blindly copy/paste what I wrote.

I tested the command by using the ls command, instead of rm… that way I could see which files would get deleted, without deleting anything!

Besides the risk of not knowing what you’re doing, the only problem with this system, is that it doesn’t seem to work if you use IMAP eg it can happen if you use smartphones/ipads/tablets/IMAP email clients… particularly if you rely on the email programs “auto-detect” system, then you probably find that the cleanup just doesn’t work.

In this case, besides some careful customer education, the only option is to use an email redirection to something like gmail , so that you use the large gmail storage, rather than the more limited hosting storage.


By Luigi Martin
Category: Linux, Technical

1 Comment »