Bulk delete email accounts from cPanel in SSH?

YouHost

Member
Apr 2, 2012
6
2
53
cPanel Access Level
Root Administrator
Hi,
I have found the following procedure to bulk delete email account for a domain.

1. Create a text file, delemails.txt, with each account name on its own line:

account_003
account_004
account_012
account_027
etc...

2. FTP that file onto your server into a convenient location.

3. SSH onto the server and cd to the location where is placed delemails.txt

4. Run this command from the SSH shell:

for i in `cat delemails.txt`; do /usr/local/cpanel/cpanel-email delpop "$i" 0 mydomain.com ; done

Do you thinks is it safe to use that?
 

24x7server

Well-Known Member
Apr 17, 2013
1,911
96
78
India
cPanel Access Level
Root Administrator
Twitter

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,810
895
313
cPanel Access Level
Root Administrator
@aadii786 - deleting the file structure would not remove the email accounts from cPanel. You'd want to use the API call to properly remove the email account:

 
  • Like
Reactions: aadii786

aadii786

Member
Feb 28, 2021
16
0
1
Pakistan
cPanel Access Level
Root Administrator
uapi --user={SPECEFIC USER NAME} Email delete_pop email='{SPECEFIC EMAIL ADDRESS}'

yes it helped me to delete email when i specifically add user name and specific email address.

uapi --user={ALL USERS IN MY SERVER} Email delete_pop email='{ALL EMAIL STARTS FROM WORD smtp @ ALL DOMAINS'

I want a loop like this please help to generate a loop or documentations I can follow to make this loop.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,810
895
313
cPanel Access Level
Root Administrator
I don't have a ready-made loop for this situation, but a "for" or "while" loop would work well for this. We have an example of this work here:

 

aadii786

Member
Feb 28, 2021
16
0
1
Pakistan
cPanel Access Level
Root Administrator
I don't have a ready-made loop for this situation, but a "for" or "while" loop would work well for this. We have an example of this work here:

Single delete: usr/local/cpanel/cpanel-email delpop {email address i want to delete} NOT WORKED

Bulk Delete: for i in `cat userslist.txt`; do /usr/local/cpanel/scripts/delpop $i ;done NOT WORKED
 

aadii786

Member
Feb 28, 2021
16
0
1
Pakistan
cPanel Access Level
Root Administrator
cat emaillist.txt | while read EMAIL;do uapi --user={cpanel username} Email delete_pop email=$EMAIL;done

Now just problem left in cPanel usernames fetching

that i have tried with 2 methods
1) extract domain name from email address that i have already list. then cPanel account name based on that extracted domain .. but no luck

I have tried regex to get domain name.
URI="http://user:[email protected]:80/"
echo $URI | sed -e '(?<[email protected])[^.]+(?=\.)'

and after getting domain name
/scripts/whoowns $DOMAIN NAMe

it helps me to get exactly what I want. but unfortunately, I'm not being able to join these methods in my above loop command. please try to help for this