cPanel .Trash Emails

So it turns out that cPanel keeps emails in the Trash forever.
Disk Usage
I was woken up at 8am this morning to find my cPanel server had died due to running out of disk space. Rookie error! I have processes in place to prevent this from happening like automatically clearing the backups once they have been safely offloaded to S3. However for some reason this hadn’t happened last night and the disk space was 100%. It was resolved pretty quickly and no harm was done, I reran the backups and all was well.

I decided to do a quick audit of the server to see if there was anything obviously taking up an abnormal amount of space. A logs directory from an CodeIgniter application was one source (full of irrelevant warnings). Another source were the .Trash folders in each Mail directory.

I had assumed that like Gmail it would delete after 30 days but no. So I did a quick search and came a cross Tom Yates who had the issue in 2012. He published a script that goes through and deletes anything that’s older than 30 days in a few directories, one being .Trash. Thanks Tom!

I had to make a few tweaks to his script to make it work on my server but here is it working on cPanel 56.

#!/bin/bash
MAILDIRS=$(find /home/*/mail/*/*/.Trash -maxdepth 0 -type d)
for basedir in $MAILDIRS; do
  for dir in cur new; do
      [ -e "$basedir/$dir" ] && (
        echo "Processing $basedir/$dir..."
        find "$basedir/$dir" -type f -mtime +30 -exec cp --parents -t  /root/deletedMail {} +
        find "$basedir/$dir" -type f -mtime +30 -delete
      )
  done
done
/scripts/generate_maildirsize --verbose --allaccounts --force --confirm

I’ve created a diectory at /root/deletedMail to copy the mail I’m about to delete (in case somebody needs it), this folder is included as part of my backup script (zipped and uploaded to S3) and gets cleared every night as part of that.

Let me know if this helped you - @florx

 
3
Kudos
 
3
Kudos

Now read this

What’s in a version?

Building distributed systems in using a microservice pattern is hard. At my company we’re always looking for ways to automate any manual processes, or anything that is difficult. Computers don’t make mistakes, but humans aren’t... Continue →