Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Mass delete e-mail accounts script

Discussion in 'Workarounds and Optimization' started by quizknows, Jan 29, 2014.

  1. quizknows

    quizknows Well-Known Member

    Joined:
    Oct 20, 2009
    Messages:
    1,010
    Likes Received:
    87
    Trophy Points:
    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=$each@$domain ; done
    else
    echo "no accounts found or other error. Check $maildir/shadow"
    exit 1
    fi
    
    rm -f /tmp/acctlist
    
    #thats all folks
    
    
     
    #1 quizknows, Jan 29, 2014
    Last edited: Jan 29, 2014
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    43,903
    Likes Received:
    1,814
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice