Linux Command Reference ยท Permissions and Users
Linux userdel command
Reference for the Linux userdel command: remove an account, delete its home directory with -r, handle logged-in users with -f, and clean up leftover files safely.
userdel removes an account from /etc/passwd, /etc/shadow and /etc/group (and, if its private group is empty, the group too). By default it leaves the home directory and the mail spool alone, which is the safe choice: the files stay on disk, owned by a numeric ID that no longer maps to a name.
With -r it also deletes the home directory and mail spool. That is permanent, so deleting an account is usually a two-step job: check what the user owns, archive anything worth keeping, then remove the account.
sudo userdel -r alice
Try it
Deleting accounts needs root. These examples show how to look for what a user owns, using a small tree you create yourself, and how orphaned files look.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
userdel [-f] [-r] [-Z] LOGIN
| Option | Description |
|---|---|
-r, --remove |
Remove the home directory and mail spool. Files elsewhere are not touched. |
-f, --force |
Remove even if the user is logged in or if another user shares the home directory. Dangerous; also removes the group if shared. |
-Z |
Remove any SELinux user mapping. |
Returns: 0 on success; 6 if the user does not exist; 8 if the user is currently logged in (without -f); 12 if the home directory could not be removed.
Usage notes
- Always find leftovers. Files outside the home directory keep the old numeric UID. Locate them with
find / -xdev -uid 1000before the account is gone (afterwards use-nouser). A lateruseraddmay reuse that UID and silently hand over the files. - Kill the user's processes first.
pkill -u alice(pgrepandpkill);userdelrefuses while a process runs, and-fdoes not clean it up. - Cron jobs and at jobs of the user are not removed. Check
/var/spool/cronandcrontab -l -u alice. - No undo.
-ris likerm -ron the home directory. Archive first. - Locking is the gentler option. If you might need the account back, use
usermod -Lplus an expiry date instead of deleting it. - Group handling. The private group is deleted only if no other user has it as primary group. Other groups lose the user as a member automatically.
deluseris the Debian wrapper (deluser --remove-home alice), which can also back up files; scripts that must be portable should useuserdel.