How To Empty A Mail Box In CPanel

How To Empty A Mail Box In CPanel

We recently received an email from a customer of ours on a un-managed VPS hosting solution. The mail in short was asking the question

How do I delete thousands of e-mail messages from one of our mail boxes in cPanel?

This is quite a common question and the number of emails can be very high. If you run a website, you may find out one day that you have a mail account containing thousands or even hundreds of thousands of messages. The most common reason this happens is because the default e-mail account is set up as a “catch-all” address. This will direct all e-mail sent to invalid usernames at your domain to the default Inbox and because spammers send out spam messages to invalid e-mail addresses all the time, the number of e-mails received by the default mailbox can grow by thousands of messages each day.

Luckily there is a easy solution to resolve this which we are going to split into 2 parts.

  • Deleting the messages from the mailbox
  • Preventing it from happening again

First lets delete all the mail in the box

Log into the Horde Mail system. (You may have other email clients available but for this we will use horde)Cpanel-Mailboxes-entry

 

 

 

Once Logged in on the left you will see the number of emails in the inbox

Horde-Mailbox-Full-1

 

 

 

 

Now Click the “Folders” icon at the top of the window.Horde-Mailbox-Full-2

 

 

 

 

 

 

Check the box next to “Inbox” or the mailbox that contains the messages you want to delete.Horde-Mailbox-Full-3

 

 

 

 

 

From the “Choose Action:” drop-down menu, select “Empty Folder(s).”Horde-Mail-Full3

 

 

 

 

 

You will be prompted to confirm the action, be careful here as it is not reversible.Horde-Mailbox-Full-4
Click “Empty Selected Folders” to clean out the mailbox.

 

 

Your Mail Has Now Been Deleted!

 

 

There is a potential issue that could arise if the mailbox has a very high volume of  messages

Depending on how many messages are stored in your Inbox, this may take up to several minutes to complete.

The only catch is that if the mailbox contains too many messages, the operation will time out and will not empty the mailbox.

This is what happened to one of our readers who had over half a million messages.

If this is also your situation, you may have to delete the message folders manually. To do this, you will first need to navigate to the mail folder, which is located in the following directory:

/home/username/mail/ (where “username” is the username for the mail account)

The two folders you’ll need to delete are the cur and new folders, which contain the messages. You may be able to do this with an SSH or SFTP program, though I also had problems with this method timing out. If you know some basic Unix commands, you can use a terminal  (command-line) program to manually delete the folders. First navigate to the mail folder, then recursively delete the cur and new directories. The commands to do this are:

$ cd /home/username/mail
$ rm -rf cur
$ rm -rf new

Again, even this method may take awhile if the directories are loaded with thousands of mail messages. But it should eventually remove the folders. This command-line method should only be used a last resort, as the maildirsize file will no longer contain the correct information for the mail account. Fortunately, as soon as a new message is received into the empty Inbox, the cur and new folders are automatically recreated and the maildirsize is updated with the correct data.

 

Now lets stop it happening again!

First log in, then click on the ‘Default Address’ icon:stop-catch-all-in-c-panel

 

 

 

 

You will then be presented with a list of option, you need to change the radio from ‘Forward to email address’ to ‘Discard with error to sender (at SMTP time)’:stop-catch-all-in-c-panel

This will then reply to all e-mails sent to addresses which have not been set up (all catch all addresses *@domain.com), the message sent can be specificied on that page but if not it will simply send ‘No such person at this address’.

As you can see, you can also discard the message totally (no reply to sender) but keep in mind if someone sends a genuine email they will not be aware they mistyped your e-mail address and thus assume .  The other option is to pipe all e-mails, this is commonly used in scripts like help desks etc (PHP, Pearl).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.