userdel is the low-level Linux command that removes a local user account by deleting its entries from /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow. In Linux permissions management, it closes the account lifecycle that begins with useradd and continues with usermod, and it requires root privileges, usually obtained through sudo.
The command belongs to the shadow-utils package (named passwd on Debian-based distributions) and acts only on accounts stored in local files. Deleting an account removes the name, but files owned by its numeric UID stay on disk unless the administrator handles them, which makes careful offboarding a security task rather than a single command.
How userdel removes an account
userdel works by editing the account databases under a lock, so concurrent tools such as passwd or usermod cannot corrupt them. It reads the login name, finds the matching records and rewrites each file without them, keeping backup copies such as /etc/passwd- for recovery.
The default run deletes only the account records. The user private group with the same name is removed as well when no other account uses it as a primary group and USERGROUPS_ENAB is set to yes in /etc/login.defs. The user is also dropped from every supplementary group listed in /etc/group.
The following table lists the files that userdel changes and the effect on each one.
| File | Content | Change made |
|---|---|---|
| /etc/passwd | Account name, UID, GID, home, shell | Line for the user is deleted |
| /etc/shadow | Password hash and aging data | Line for the user is deleted |
| /etc/group | Group names and member lists | User removed from member lists |
| /etc/gshadow | Group passwords and administrators | User removed from entries |
| /etc/subuid and /etc/subgid | Subordinate ID ranges for containers | Ranges for the user are released |
| Home directory and mail spool | Personal files and mail | Deleted only with the -r option |
Key userdel options and their effects
userdel has a small, deliberately conservative option set, because each option increases the amount of irreversibly destroyed data. The most consequential choice is whether to pass -r, which turns a record change into a file deletion operation.
| Option | Effect | Typical use |
|---|---|---|
| -r, –remove | Deletes home directory and mail spool | Final removal after data is archived |
| -f, –force | Removes the account even if the user is logged in | Emergency removal of a compromised account |
| -Z, –selinux-user | Removes the SELinux user mapping | Systems that enforce SELinux policy |
| -R, –root CHROOT_DIR | Applies changes inside a chroot directory | Preparing system images offline |
| -P, –prefix PREFIX_DIR | Uses account files under a prefix path | Editing mounted or alternate root trees |
Without -f, userdel refuses to remove an account that has running processes, returning exit status 8. This protection prevents a half-removed state in which processes keep running under a UID that no longer maps to a name. Even -r deletes only the home directory and the mail spool, so files elsewhere on the system remain and still carry the old UID.
userdel compared with related account tools
userdel is one of several tools that manage accounts, and choosing the right one depends on whether removal must be permanent. Locking an account with usermod or passwd often serves offboarding better, because it preserves ownership information for audits.
| Tool | What it does | Reversible |
|---|---|---|
| userdel | Deletes account records, optionally home data | No, without backups |
| deluser | Debian wrapper that adds backup and policy options | No, but can archive files first |
| usermod -L -e 1 | Locks the password and expires the account | Yes |
| passwd -l | Locks only password authentication | Yes, but SSH keys may still work |
| groupdel | Deletes a group, not a user | No |
A frequent misconception is that passwd -l fully disables a user. It only prefixes the hash with an exclamation mark, so key-based SSH logins can still succeed. Setting an expiry date in the past or changing the shell to /usr/sbin/nologin blocks such access more reliably.
Safe offboarding with userdel
Safe use of userdel follows a fixed offboarding sequence: disable, inventory, archive, terminate processes and only then delete. Running these steps in order avoids lost data and orphaned files that later surprise other users.
- Lock the account with
usermod -L -e 1 aliceand remove its SSH keys. - List the files it owns with
find / -xdev -user aliceon every mounted file system. - Archive the home directory and any important owned files.
- Stop its processes and remove its cron jobs and at jobs.
- Run
userdel -r aliceand verify the result withgetent passwd alice.
The decision table below matches common situations to a recommended approach.
| Situation | Recommended action | Reason |
|---|---|---|
| Employee leaves, data needed later | Lock now, archive, delete later | Keeps evidence and files available |
| Compromised account | Lock, kill processes, then userdel -f | Stops access immediately |
| Temporary test account | userdel -r | Nothing worth keeping |
| Service account with owned data | Reassign files with chown first | Prevents orphaned ownership |
| Account from LDAP or FreeIPA | Remove it in the directory service | userdel edits local files only |
Risks, orphaned files and common mistakes
The most serious risk of userdel is UID reuse. Files left behind keep the old numeric owner, and when useradd later assigns the same UID to a new person, that person silently inherits ownership of the old files, including private data. Many distributions avoid immediate reuse by allocating the next highest UID, but gaps can still be filled.
| Mistake | Consequence | Remedy |
|---|---|---|
| Deleting without archiving | Permanent data loss | Back up home and owned files first |
| Ignoring files outside home | Orphaned files show only a number | Search with find -nouser and reassign |
| Forcing removal of a busy user | Processes run under an unnamed UID | Kill processes before deletion |
| Removing a shared primary group | Group ownership becomes numeric | Check members of the group first |
| Editing /etc/passwd by hand | Inconsistent shadow and group files | Use userdel or vipw with care |
Administrators should also remember that userdel does not revoke sudo rights defined by user name in sudoers files, nor entries in a Linux ACL. Stale names in these rules can later grant privileges to a new account that receives the same name, so auditing sudoers and ACLs belongs in every removal checklist.
The idea in brief
Think of a Linux computer as an office building with a guest list. userdel is the tool that crosses a person off that list, so they can no longer sign in. By default, it leaves their desk and boxes where they are; with one extra option it also clears out their personal locker.
The catch is that anything they left elsewhere in the building still carries their old badge number. If a newcomer later gets the same number, those leftovers suddenly belong to the newcomer. That is why careful administrators lock the account, save the files and tidy up first, and only then remove the person for good.
A practical scenario
A university research group runs a shared Ubuntu server for twelve doctoral students. When one student, with the login mkovac and UID 1007, finishes the degree, the system administrator first runs usermod -L -e 1 mkovac and deletes the student’s authorized SSH keys, so access ends that afternoon.
A search with find / -xdev -user mkovac reveals 40 GB of simulation results in /data/projects in addition to the home directory. The administrator archives the home directory to the group’s backup storage, changes ownership of the project data to the supervisor with chown, and removes the student’s crontab and a stale sudoers entry.
Two weeks later, after the supervisor confirms that nothing is missing, userdel -r mkovac removes the account. A final find / -nouser returns nothing, so no orphaned files remain for a future account with UID 1007 to inherit.