userdel
Delete a user account
By CMD Script Team · 4 min read · Last updated
userdel [OPTIONS] USERNAMEOptions
| Flag | Description |
|---|---|
-r | Remove the user's home directory and mail spool along with the account |
-f | Force removal even if the user is still logged in or files are owned elsewhere |
-Z | Remove any SELinux user mapping for the account's login |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (not available; use dscl -delete instead)
What it does
userdel removes a user account by deleting its entries from /etc/passwd,
/etc/shadow, and the user's own primary group entry in /etc/group if one exists. By
default it leaves the user's home directory and files elsewhere on disk untouched — you
must explicitly ask it to clean those up.
Beginner examples
sudo userdel alice— remove the account, but leave the home directory in placesudo userdel -r alice— remove the account and delete the home directory and mail spoolid alice(before deleting) — confirm the account exists and check its UIDwhoorw— check whether the user is currently logged in before deleting
sudo userdel -r alice
Advanced examples
- Force-remove a user even though they have an active session or owned processes:
sudo userdel -rf alice(use cautiously — orphaned processes can behave unpredictably). - Audit for leftover files after deletion by UID rather than username, since the name no
longer resolves:
sudo find / -xdev -uid 1500 -print(using the old numeric UID). - Remove an SELinux user mapping alongside the account on SELinux-enabled systems:
sudo userdel -Z alice. - Combine with
groupdelwhen the user had a dedicated personal group that's now empty and unused:sudo groupdel alice(only if no other user relies on that group).
sudo userdel -r alice && sudo find / -xdev -uid 1500 -print
Common mistakes
- Forgetting
-rand assuming the account is fully gone, when the home directory and mail spool are actually still sitting on disk consuming space. - Using
-fcasually on a user with active processes, which can force-remove the account while leaving orphaned running processes tied to a UID that no longer maps to a name. - Not checking for files owned by the user outside their home directory (e.g. in
/var/www,/opt, shared project folders) —userdel -ronly cleans up the home directory and mail spool, nothing else. - Deleting a user whose UID is still referenced in cron jobs, systemd services, or file ownership elsewhere, causing "unknown user" errors or orphaned ownership afterward.
Tips
- Run
whoorwfirst to check if the user has an active session before deleting, rather than jumping straight to-f. - Search for files owned by the user's UID system-wide before deleting, so you can decide
whether to reassign, archive, or delete them:
find / -user alice. - Consider locking the account first (
usermod -L aliceorpasswd -l alice) as a reversible step before permanently deleting it, especially in production.
Best practices
- Always audit for and handle files owned by the user outside their home directory
before or after running
userdel -r, to avoid orphaned ownership. - Avoid
-fas a routine option; investigate why a user still has active sessions or processes rather than force-removing through it. - In production, prefer a "lock, wait, then delete" workflow (disable login, verify
nothing depends on the account, then
userdel -r) over immediate deletion, to reduce the blast radius of mistakes.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Offboarding an employee: locking their account first, then later running
userdel -ronce all their data has been reviewed or migrated. - Cleaning up temporary or contractor accounts created for a short-term project.
- Removing stale service-like accounts left over from decommissioned applications, after confirming no cron jobs or processes still depend on them.
Common interview questions
- What's the difference between
userdelanduserdel -r? Plainuserdelonly removes the account entries from/etc/passwd//etc/shadow//etc/group, leaving the home directory and mail spool in place;-ralso deletes those. - What happens to files a deleted user owned outside their home directory? They're
left untouched, now owned by a numeric UID with no corresponding account name, and show
up that way in
ls -luntil reassigned or removed. - Why might
userdelrefuse to delete an account? If the user has an active login session or running processes,userdelrefuses by default;-foverrides this but can leave orphaned processes.
Frequently Asked Questions
What happens to a user's files if I don't use -r?
Without -r, userdel only removes the account entries (from /etc/passwd, /etc/shadow, /etc/group) but leaves the home directory, mail spool, and any files owned by that user elsewhere on the filesystem untouched. They become orphaned, owned by a now-nonexistent UID.
Can I delete a user who is currently logged in?
By default userdel refuses to delete a user with an active login session or running processes. Use -f to force it, but be aware this can leave processes running under a UID with no matching account, which is best avoided in production.
Does userdel remove files owned by the user outside their home directory?
No. userdel -r only removes the home directory and mail spool. Files elsewhere on the system that the user owns are left behind, now owned by a UID that no longer maps to any account name (they show up as a numeric UID in ls -l).
Cheat sheet
Download a quick-reference cheat sheet for userdel.