How to delete a subdomain dynamically using PHP

If you are looking to delete a subdomain dynamically using PHP, then here is how to do it:

  1. Port number 2082 must be open for cPanel Installation.
  2.  Get cPanel skin name. This you can find out from the URL after logging in to cPanel. let’s say URL looks like this  Then paper_lantern is the skin name.
  3. Here is the snippet to delete the code:


$cpanel_user = “cpanel_username”;

// your cPanel password. Replace ‘password; with your cPanel password.

$cpanel_pass = “password”;

// your cPanel skin. Replace x2 with your cpanel skin in use.

$cpanel_skin = “paper_lantern”;

// your cPanel domain

$cpanel_host = “”;
$subdomain = “testing”;
function delete_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain)
$buildRequest = “/frontend/x3/subdomain/dodeldomain.html?domain=” . $subDomain . “_” . $rootDomain;

$openSocket = fsockopen(‘localhost’,2082);
if(!$openSocket) {
return “Socket error”;

$authString = $cPanelUser . “:” . $cPanelPass;
$authPass = base64_encode($authString);
$buildHeaders = “GET ” . $buildRequest .”\r\n”;
$buildHeaders .= “HTTP/1.0\r\n”;
$buildHeaders .= “Host:localhost\r\n”;
$buildHeaders .= “Authorization: Basic ” . $authPass . “\r\n”;
$buildHeaders .= “\r\n”;

fputs($openSocket, $buildHeaders);
while(!feof($openSocket)) {

$passToShell = “rm -rf /home/” . $cPanelUser . “/public_html/subdomains/” . $subDomain;

delete_subdomain($subdomain, $cpanel_user, $cpanel_pass, $cpanel_host);

echo “<br/>”;
echo “Deleted $subdomain”;



This snippet will help you to delete the subdomain which you no longer want.

Thanks for dropping by!! Feel free to comment to this post or drop me an email to [email protected].