Auto Delete Trash Folders on cPanel Servers

If you are running Dovecot on your cPanel servers, you’ll have noticed that mail trash isn’t auto-deleted which is bloody annoying for you and for your customers.

I’ve found this script on the cPanel forum that will delete email trash that is over 30 days old.

MAILDIRS=$(find /home/*/mail/*/* -maxdepth 0 -type d)
INBOXFOLDERS=(.Trash .Junk .Spam .Low\ Priority .cPanel\ Reports)
for basedir in $MAILDIRS; do
  for ((i = 0; i < ${#INBOXFOLDERS[*]}; i++)); do
    for dir in cur new; do
      [ -e "$basedir/${INBOXFOLDERS[$i]}/$dir" ] && (
        echo "Processing $basedir/${INBOXFOLDERS[$i]}/$dir..."
        find "$basedir/${INBOXFOLDERS[$i]}/$dir/" -type f -mtime +30 -delete
/scripts/generate_maildirsize --verbose --allaccounts --force

To run, SSH into your server and create a file such as and make sure you give it executable permissions (chmod +x) and then run ./

I’d add it to a daily cron.

I’ve also put the script online site so you can use wget on the server to save you doing anything, I’ve put it in the /root folder

cd /root
chmod +x
#add to crontab 
(crontab -l; echo "30 1 * * * /root/ > /dev/null 2>&1") | crontab
#run for first time