Removing an Addon domain with commands / ssh / script

jonahnaylor

Registered
Jul 25, 2011
4
0
51
Hi hopefully someone can help. I'm writing a script to remove an addon domain but so far it's not getting rid of it...

Currently I've just tried to use the safer cpanel scripts and a few commands to achieve this but I'm hoping some can fill in the gaps as to what I'm missing so that when I navigate back into that account after running the script the domain is no longer there under the Addon Domains section. Any help appreciated, here is what I have so far:

Code:
set -x                                                                  # Turns tracing on so we can see what is happening
ex +g/$addonDOMAIN/d' -cwq /var/cpanel/users/$cpanelACC                 # Remove all lines containing addon domain in users file
ex +g/$addonDOMAIN/d' -cwq /var/cpanel/userdata/$cpanelACC/main         # Remove all lines containing addon domain in userdata file

/usr/local/cpanel/scripts/killdns $addonDOMAIN                          # Remove Addon Domain from BIND
/usr/local/cpanel/scripts/killvhost $addonDOMAIN                        # Remove Addon Domain http entries
/usr/local/cpanel/scripts/rebuildhttpdconf                              # Rebuild Apache Configuration
set +x                                                                  # Turns tracing off again

APACHE_CONFIG=$(/usr/local/apache/bin/apachectl configtest 2>&1)        # Check condition of Apache with a test

if [[ $APACHE_CONFIG =~ "Syntax OK" ]]; then
    echo "Apache config is ok. Restarting Apache."

    RESTART_APACHE=$(/usr/local/cpanel/scripts/restartsrv httpd)

    if [[ $RESTART_APACHE =~ "Apache successfully restarted" ]]; then
        echo "Apache restarted successfully"
    else
        echo "Apache restart failed: $RESTART_APACHE"
    fi
else
    echo "Apache config problem. Will not restart apache: $APACHE_CONFIG"
fi
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,222
463
Hello,

It looks like you are attempting to manipulate configuration files through the script. I suggest using cPanel API 2 instead. Here's a cPanel API 2 function you can use:

cPanel API 2 Functions - AddonDomain::deladdondomain - Software Development Kit - cPanel Documentation

You may also find this document helpful:

Guide to cPanel API 2 - Software Development Kit - cPanel Documentation

Note that cPanel UAPI is recommended over cPanel API 2, however no equivalent UAPI function exists at this time.

Thank you.