SpamAssassin Spam Box Script Empty

How do we keep this site running? This post may contain affiliate links — the cost is the same to you, but we get a referral fee. Compensation does not affect rankings. Thanks!

SpamAssassin’s spam box feature is great – it captures spam on the server before it even has a chance to hit your mail client. The only problem is that unless emptied, it quickly grows in size. It will eventually take up too much server space.

Why don’t you simply empty the spam box?

The problem with emptying the spam box is that it clears the entire box out in an instant. It must also be done manually. This can prove quite tedious. Wouldn’t it be much more practical to simply have it clear out any spam mails that are older than 30 days? This would keep SpamAssassin’s spam box size within a reasonable limit, and still allow enough time for you or your clients to check spam mails, to make sure a legitimate message didn’t get caught by accident.

Script to empty SpamAssassin spam box of all spam older than 30 days

Well, here it is. This script will empty the spam box of all mail older than 30 days. Feel free to adjust the variables to suit your own needs (for example, to keep spam mail longer or delete it sooner):

find /home/*/mail/.*/.spam/new -mtime +30 -exec rm {} \;
find /home/*/mail/.*/.spam/cur -mtime +30 -exec rm {} \;

You may need to update the path depending on the directory structure of your server setup (for this example, we’re using Cpanel 10 and the path to the home directory is /home/username/mail/.name@example_com/.spam/new).

How to empty the spam box based on its size

If you’d rather, or also, like to check for folder size, you can modify the script with the following addition which also checks for size (in this example, 10 Megabytes):

find /home/*/mail/.*/.spam/new -mtime +30 -size +10M -exec rm {} \;

Table for size parameters

Here’s various sizes you can plug into the script:

-size n[cwbkMG] File uses n units of space. The following suffixes can be used:

  • ‘b’ for 512-byte blocks (this is the default if no suffix is used)
  • ‘c’ for bytes
  • ‘w’ for two-byte words
  • ‘k’ for Kilobytes (units of 1024 bytes)
  • ‘M’ for Megabytes (units of 1048576 bytes)
  • ‘G’ for Gigabytes (units of 1073741824 bytes)

The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated. Bear in mind that the `%k’ and `%b’ format specifiers of -printf handle sparse files differently. The `b’ suffix always denotes 512-byte blocks and never 1 Kilobyte blocks, which is different to the behaviour of -ls.

Alex bring a series of in-depth articles on search marketing and content management systems as well as troubleshooting tips to We Rock Your Web's collection. He is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs, Bella and Lily.

Leave a Reply

3 Comments on "SpamAssassin Spam Box Script Empty"


I think I’m missing something here. How do I implement this script?

We Rock Your Web
We Rock Your Web

Assuming you’re on an Apache (Linux/UNIX) server, simply enter your Control Panel (Cpanel) and create a cron job. Then run the above code.


Thank you so much for this post. I have been searching for and wide for a script that will do exactly this (found one on Experts Exchange but didn’t want to pay to see the answer), to prevent my host (on a shared server) from shutting down my site due to spam overload (in a root email account I can’t even control).

SpamAssassin may be fine once it’s trained, but that takes an eternity. I can’t believe this function is not native to Cpanel. On high volume sites it doesn’t take long for spam to take up a bunch of space, especially if attachments are involved.


Send this to a friend