All posts

How to empty cPanel trash folders safely (and find what else is filling /home)

When a customer deletes files in cPanel's File Manager, they usually don't go away. By default File Manager moves them to a hidden .trash folder in the account's home directory, where they sit until someone empties it. On a server with a few hundred accounts, those folders can add up to a surprising share of /home, and nobody is looking at them, because the files are "deleted".

This guide finds every trash folder on the server, shows how to check what's in them, and empties them in a way that can't go wrong if an account has done something odd with symlinks. Everything up to the cleanup step is read-only.

How cPanel's trash works

  • File Manager's Delete moves files to ~/.trash unless the user ticks "Skip the trash and permanently delete the files".
  • File Manager keeps a restore index in ~/.trash_restore, which is how Restore knows where each file came from.
  • Trashed files are still in the account's home directory, so they still count against its disk quota and still take space on the partition.
  • Nothing empties the trash on a schedule. It's emptied when the user clicks Empty Trash in File Manager, or when someone deletes it by hand.

Step 1: Measure every account's trash

As root, list the trash folders by size:

for d in /home*/*/.trash; do
  [ -d "$d" ] && [ ! -L "$d" ] && du -sh "$d" 2>/dev/null
done | sort -rh | head -20

The [ ! -L "$d" ] test skips any .trash that is a symlink rather than a real folder. You'll see why that matters in Step 4.

For a server-wide total:

du -shc /home*/*/.trash 2>/dev/null | tail -1

If the total is small, you're done here; skip to Step 5 for the other places space hides.

Step 2: Look before you delete

Trash belongs to the customer. Some people use it as a "maybe" folder, and a restore request after you've emptied it is a bad conversation. Before deleting anything, look at what's there:

# Largest files in one account's trash
find /home/USER/.trash -type f -printf '%s\t%TY-%Tm-%Td\t%p\n' 2>/dev/null \
  | sort -rn | head -20 | numfmt --field=1 --to=iec

The date column is the file's modification time, not the date it was trashed, so an old date doesn't prove the file was deleted long ago.

Things worth noticing:

  • Full-account backups (backup-*.tar.gz) and site archives. These are often the biggest items, and often the ones the customer meant to delete.
  • Whole site folders. A public_html copy in the trash usually means a site was rebuilt.
  • Recent, large uploads. These are the files most likely to be missed if they were trashed by mistake.

How you handle this is a policy decision, not a technical one: a notice to customers, a size or age threshold, or emptying trash only for accounts you manage yourself. Decide it once and write it down.

Step 3: Record where you started

Take a "before" reading so you can show what the cleanup recovered:

df -h /home
du -sh /home/USER/.trash

Step 4: Empty it as the account's user, not as root

The obvious command is rm -rf /home/USER/.trash/*, run as root. It usually works. The problem is the case where it doesn't: the folder is writable by the account, so the account controls what's in it. If .trash (or something inside it) is swapped for a symlink to somewhere else, a root process walking that tree can end up deleting files the account never owned.

The fix is to not use root for the deletion. Run it as the account's own user, so the kernel only lets it delete what that user could delete anyway:

ACCT=example   # the cPanel account name
runuser -u "$ACCT" -- find "/home/$ACCT/.trash" -mindepth 1 -delete
  • runuser needs root to start, then drops to the account's user. It doesn't depend on the account's login shell, so it works for accounts with noshell or jailshell.
  • -mindepth 1 keeps the .trash folder itself and removes only its contents.
  • find -delete doesn't follow symlinks; a symlink inside the trash is removed as a link.

Then clear File Manager's restore index, so it doesn't list files that no longer exist:

[ -f "/home/$ACCT/.trash_restore" ] && [ ! -L "/home/$ACCT/.trash_restore" ] \
  && runuser -u "$ACCT" -- truncate -s 0 "/home/$ACCT/.trash_restore"

Check the result against your "before" numbers:

du -sh /home/USER/.trash
df -h /home

If you want to empty several accounts, loop over a list you've reviewed, not over every folder on the server:

for ACCT in alice bob carol; do
  T="/home/$ACCT/.trash"
  [ -d "$T" ] && [ ! -L "$T" ] || continue
  runuser -u "$ACCT" -- find "$T" -mindepth 1 -delete
done

Adjust /home if your accounts live on /home2 or another partition (grep "^$ACCT:" /etc/passwd | cut -d: -f6 shows an account's home directory).

Step 5: The other usual suspects in /home

Trash is one place space hides. These are the others worth a read-only look while you're in there.

Account backups left in the home directory. cPanel's Backup Wizard writes backup-<date>_<user>.tar.gz into the account's home directory, and they tend to stay there:

find /home*/*/ -maxdepth 1 -type f -name 'backup-*.tar.gz' -size +100M \
  -printf '%s\t%p\n' 2>/dev/null | sort -rn | head -20 | numfmt --field=1 --to=iec

Runaway error_log files. PHP writes an error_log into the folder of the script that failed, so a noisy site can grow one in every directory:

find /home*/*/public_html -type f -name error_log -size +50M \
  -printf '%s\t%p\n' 2>/dev/null | sort -rn | head -20 | numfmt --field=1 --to=iec

Fix the warning that fills it before you truncate it, or it will be back next week.

Application backup folders, for example softaculous_backups if your server runs Softaculous:

du -sh /home*/*/softaculous_backups 2>/dev/null | sort -rh | head -20

Mail. Mail folders have their own Trash, and it isn't the same thing as File Manager's trash. Customers can see and clean their mailboxes in cPanel under Email Disk Usage. Treat mail as the customer's to manage.

None of these are safe to delete across the board. Like the trash, list them, check with the owner where it matters, then clean up.

Doing this from WHM

If you'd rather not run these loops by hand on every server, Server Commander for cPanel & WHM covers this in its Large File Finder. As of version 8.2.0:

  • A Stats panel groups what it finds into cPanel trash cans, backups and archives, logs, media and databases, with a category filter to show one group at a time.
  • Toggles hide the usual noise: jailed virtfs mounts, /backup folders, MySQL and system files, and wp-content.
  • Empty Trash Cans measures every account's ~/.trash, lists the accounts with anything in them by size and file count, and lets you untick any account you want to skip. It deletes as each account's own user, clears File Manager's restore index, and shows the files deleted, the space recovered, and /home free space before and after.

It's the same approach as this guide, with the before-and-after reporting built in. Server Commander is $19.99/month per server, or part of the Server+ Full Suite.