Auto Delete Trash Folders on cPanel Servers Using Dovecot

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.

#!/bin/bash
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
      )
    done
  done
done
/scripts/generate_maildirsize --verbose --allaccounts --force

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

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
wget http://tomyates.co.uk/auto_delete_spam.sh
chmod +x auto_delete_spam.sh
#add to crontab 
(crontab -l; echo "30 1 * * * /root/auto_delete_spam.sh > /dev/null 2>&1") | crontab
#run for first time
/root/auto_delete_spam.sh

source: http://tomyates.co.uk/2012/08/01/auto-delete-trash-folders-on-cpanel-servers-using-dovecot/

Was this helpful?

Next Article

Auto Responders