Managing hosting accounts efficiently is critical for server administrators and hosting providers. Deleting multiple cPanel accounts manually can be tedious, but with the WHM API and a simple script, you can automate the process and save valuable time.
Why Automate Account Deletion?
Automating account deletion:
- Saves time when managing multiple accounts.
- Reduces the risk of human error.
- Provides a repeatable process for bulk tasks.
Prerequisites
Before diving into automation, ensure:
- You have root access to the server.
- WHM API is enabled.
- You understand basic shell scripting.
Step-by-Step Guide
1. Prepare a List of Usernames
Create a list of sample usernames representing the cPanel accounts to delete. For example:
user1
user2
user3
user4
user5
2. Create the Shell Script
This script loops through each username and deletes the corresponding cPanel account using the whmapi1 removeacct
command.
Here’s a sample script:
#!/bin/bash
# Sample list of usernames to delete
usernames=(
"user1"
"user2"
"user3"
"user4"
"user5"
)
# Loop through the usernames and delete each account
for username in "${usernames[@]}"; do
echo "Attempting to delete account: $username"
whmapi1 removeacct user="$username"
if [ $? -eq 0 ]; then
echo "Successfully deleted account: $username"
else
echo "Failed to delete account: $username"
fi
done
3. Save and Execute the Script
- Save the script as
delete_accounts.sh
. - Make the script executable:
chmod +x delete_accounts.sh
- Run the script as root:
./delete_accounts.sh
4. Verify the Deletion
After running the script, check the WHM interface or use the whmapi1 listaccts
command to verify that the accounts have been removed.
Important Tips
- Use sample data during testing. Ensure your script works as expected before using real data.
- Back up all data. Always create backups of critical accounts before deletion.
- Double-check usernames. Review your list carefully to avoid accidental deletions.
Conclusion
Automating cPanel account deletion with the WHM API streamlines your server management tasks. Whether you’re cleaning up unused accounts or performing bulk deletions, this script can save time and minimize errors.
Feel free to test this script with sample data and share your experience in the comments below. Happy automating! 🚀
Tags:
- cPanel
- WHM API
- Automation
- Hosting Management
- Shell Scripting