quizknows

Well-Known Member
Oct 20, 2009
1,008
87
78
cPanel Access Level
DataCenter Provider
I had a customer who moved a domain to Google's MX servers, and needed the old accounts removed from his system.

The only "good" search result on google for this was this very old thread I can no longer post in:
http://forums.cpanel.net/f43/mass-delete-e-mail-accounts-130001.html

This still requires a lot of manual work.

I wrote a bash script for this as it was way faster than removing all the accounts by hand. While this script should be safe, I advise you review it and have backups of your accounts before using it.

This will allow you to quickly and easily remove every e-mail account under one particular domain.

Paste it into massemailremove.sh, chmod it to executable, and make sure you really want to do this.

Code:
#!/bin/bash
if [ -z $2 ]
then
echo "Usage: massemailremove.sh USERNAME DOMAIN"
exit 0
fi

#define username/domain
username=$1
domain=$2

#define mail directory
eval homedir="$(printf "~%q" "$username")"

maildir=$homedir/etc/$domain

#sanity check, make sure directory exists in expected location. 
if [ -d $maildir ]
then
echo "$maildir exists in expcted location"
else echo "$maildir does not exist. Check username and domain are correct, and that e-mail accounts exist for this domain. Exiting"
exit 1
fi

#ARE YOU SURE???
echo "this will PERMANANTLY remove ALL e-mail accounts from $domain."
echo "are you SURE you want to do this? Type YES in all caps to confirm."
read confirmation
if [[ "$confirmation" == "YES" ]]
then
echo confirmed
else
echo "Did not confirm, exiting"
exit 0
fi

#one more sanity check
if [ -x /scripts/delpop ]
then
echo "/scripts/delpop exists. Continuing."
else
echo "/scripts/delpop not found on your system. Make sure you are running a current cPanel version"
exit 1
fi

#the real "meat" of the script. Point of no return.
/bin/cut -d':' -f 1 $maildir/shadow > /tmp/acctlist

if [ -s /tmp/acctlist ]
then
for each in `cat /tmp/acctlist` ; do /scripts/delpop [email protected]$domain ; done
else
echo "no accounts found or other error. Check $maildir/shadow"
exit 1
fi

rm -f /tmp/acctlist

#thats all folks
 
Last edited:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,261
463
Hello :)

Thank you for sharing this workaround. Please note that user-submitted workarounds are not tested or supported by cPanel. We encourage everyone to review all aspects of workarounds before implementing them on a production server.

Thank you.