Skip to content

Cleaning up orphaned cPanel users after an account move

An account move can leave system users, mail directories, MySQL grants, and cron entries behind. Here is how to identify and remove the orphans without breaking active accounts.

You ran /scripts/killacct on a source server after a successful transfer, or used WHM’s “Terminate an Account.” Months later, repquota lists a UID that has no /etc/passwd entry, MySQL has grants for a user that no longer exists, or /etc/cron.d/ still runs jobs as a non-existent UID. The account is gone from cPanel’s point of view but its footprint persists in the host’s state.

What’s happening

cPanel account termination is a coordinated sequence: remove the user, drop databases, delete mail, clear DNS, remove cron, retire the certificate. Any one of those steps can fail silently — a database the user did not own but had grants on, a mail directory mounted from a different filesystem, a cron job written directly into /var/spool/cron/USER while a process held the file open, a custom integration that wrote to /etc/aliases.d/USER outside cPanel’s tracking. The cPanel user file is gone, but the kernel still tracks blocks against the UID, MySQL still has the grant, and the cron daemon still tries to fire jobs that fail to log because there is no shell.

How to verify

Find UIDs with disk usage but no passwd entry:

repquota -avs | awk '{print $1}' | while read u; do
  getent passwd "$u" >/dev/null 2>&1 || echo "ORPHAN: $u"
done

Find MySQL users with no corresponding cPanel account:

mysql -BN -e "SELECT User FROM mysql.user WHERE User NOT IN ('root','mysql.sys','mysql.session','debian-sys-maint','horde','roundcube')" | \
  while read u; do
    cpanel_user=$(echo "$u" | cut -d_ -f1)
    [ -d "/var/cpanel/users/$cpanel_user" ] || echo "ORPHAN MYSQL: $u (cpanel=$cpanel_user)"
  done

Find stray cron entries:

for f in /var/spool/cron/*; do
  u=$(basename "$f")
  getent passwd "$u" >/dev/null 2>&1 || echo "ORPHAN CRON: $f"
done

And check /etc/userdomains, /etc/userdatadomains, /etc/trueuserdomains for entries pointing to non-existent users.

The fix

  1. Before removing anything, snapshot the affected paths or run /scripts/pkgacct USER on the destination first to confirm the migration is genuinely complete. Orphan cleanup is irreversible.
  2. Remove orphaned MySQL users with DROP USER 'name'@'localhost'; then FLUSH PRIVILEGES. Drop databases owned by no cPanel user only after confirming with the customer or your CMDB.
  3. Delete orphan cron files: rm /var/spool/cron/ORPHAN_USER then systemctl reload crond. Check /etc/cron.d and /etc/cron.hourly for hand-edited entries.
  4. Run /scripts/updateuserdomains to rebuild the userdomain map and surface any inconsistencies. Then /scripts/fixquotas to clear orphan UID accounting.
  5. Reclaim the home directory: rm -rf /home/ORPHAN_USER only after a final find for unexpected files (custom integrations sometimes write under another tenant’s home). Use lsof +D /home/ORPHAN_USER first to ensure nothing is reading from it.

The cleanup itself is mechanical. The risk is removing something that still belonged to a live account through a misnaming collision — verify before deleting.

If a fleet of cPanel servers has accumulated orphan state over years of migrations, that is a remediation engagement we run inside Deployment Management. Get in touch.