Last active
March 19, 2025 02:10
-
-
Save cipulan/e14fb4b1a34f81fa52b3a512874b6782 to your computer and use it in GitHub Desktop.
Bulk Delete Sub Domain on cPanel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define the cPanel username | |
cpanel_user="your_cpanel_username" | |
# Define the file containing the list of subdomains | |
file="domains.txt" | |
# Define the log file | |
log_file="deletion_log.txt" | |
# Maximum number of retries for a failed command | |
max_retries=3 | |
# Function to delete a subdomain and its document root | |
delete_subdomain() { | |
local domain=$1 | |
local attempt=1 | |
local success=0 | |
while [ $attempt -le $max_retries ]; do | |
# Get the current timestamp | |
timestamp=$(date +"%Y-%m-%d %H:%M:%S") | |
# Fetch document root before deleting the subdomain | |
docroot=$(uapi --output=jsonpretty --user="$cpanel_user" DomainInfo domains_data | jq -r ".result.data.sub_domains[] | select(.domain==\"$domain\") | .documentroot") | |
if [ -z "$docroot" ]; then | |
echo "[$timestamp] ERROR: Could not retrieve document root for $domain" | tee -a "$log_file" | |
return | |
fi | |
# Run the cpapi2 command and capture output and status | |
output=$(/usr/local/cpanel/bin/cpapi2 --user="$cpanel_user" SubDomain delsubdomain domain="$domain" 2>&1) | |
status=$? | |
if [ $status -eq 0 ]; then | |
echo "[$timestamp] SUCCESS: Deleted subdomain $domain" | tee -a "$log_file" | |
success=1 | |
break | |
else | |
echo "[$timestamp] ERROR: Attempt $attempt failed for subdomain $domain" | tee -a "$log_file" | |
echo "[$timestamp] Output: $output" | tee -a "$log_file" | |
if echo "$output" | grep -q "signal 14 (ALRM)"; then | |
echo "[$timestamp] Retrying due to signal 14 (ALRM) error..." | tee -a "$log_file" | |
fi | |
fi | |
attempt=$((attempt + 1)) | |
sleep 2 | |
done | |
if [ $success -eq 1 ]; then | |
# Delete the document root if the subdomain was successfully removed | |
if [ -d "$docroot" ]; then | |
rm -rf "$docroot" | |
echo "[$timestamp] SUCCESS: Deleted document root $docroot" | tee -a "$log_file" | |
else | |
echo "[$timestamp] WARNING: Document root $docroot does not exist" | tee -a "$log_file" | |
fi | |
else | |
echo "[$timestamp] ERROR: All attempts failed for subdomain $domain" | tee -a "$log_file" | |
fi | |
} | |
# Loop through each line in the file | |
while IFS= read -r domain; do | |
delete_subdomain "$domain" | |
done < "$file" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub1.example.com | |
sub2.example.com | |
sub3.example.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Subdomain Deletion Script with Error Handling and Logging
This Bash script automates the process of deleting subdomains using the cPanel API (cpapi2). It reads subdomains from a text file and executes the deletion command for each entry.
How to Use
Download the Script:
Save the script as
delete_subdomains.sh
.Adjust File:
Modify file
delete_subdomains.sh
line 4 onyour_cpanel_username
with your actual cPanel UsernameMake It Executable:
Run the following command to grant execute permissions:
Prepare the Subdomains List:
Create or update the domains.txt file in the same directory as the script.
Run the Script:
Execute the script with:
The script logs all activities in deletion_log.txt. This includes timestamps, success messages, error messages, and debug output for failed attempts.