TechEarl

How to Delete a User on Linux (userdel)

Remove a user with userdel, including the home directory and mail spool with -r, plus what to do about files they owned elsewhere on the system.

Ishan Karunaratne⏱️ 4 min readUpdated
Share thisCopied
Delete a Linux user with userdel, remove the home directory and mail spool with -r, and handle files they left elsewhere.

Deleting a user is a root task. The decision is whether to keep their files. To remove the account and its home directory and mail spool:

bash
sudo userdel -r deploy
Root terminal running userdel -r deploy removing the home and mail spool, then id deploy returning 'no such user' confirming the account is gone.
userdel -r removes the account plus its home directory and mail spool. id then reports no such user.

Without -r, userdel deploy removes the account but leaves /home/deploy behind. That is sometimes what you want (keep their work, free the login), but more often it just leaves orphaned files owned by a now-missing UID.

Before you delete: check for running processes and shared files

A user can own files and processes all over the system, not just in their home directory. Deleting the account leaves those owned by a bare numeric UID, which a future new user could inherit.

bash
# First, check: are they logged in or running anything?
who | grep deploy
ps -u deploy

# Then, if you are sure, end their processes
sudo pkill -u deploy

# Find everything they own outside their home
sudo find / -user deploy -not -path '/proc/*' 2>/dev/null

userdel refuses to remove a user who is currently logged in or has running processes, which is a feature; check with who/ps, then end the processes first (or use userdel -f to force, which is riskier). The find sweep is the step people skip and regret. Reassign or remove what it finds before (or after) deleting the account. The owner-search syntax is covered in find files by owner and permission.

Lock instead of delete (often the better move)

For a departing team member, deleting the account immediately can break cron jobs, mail, and file ownership. Locking it keeps everything in place while blocking access:

bash
sudo usermod -L deploy          # lock the password
sudo usermod -s /usr/sbin/nologin deploy   # block the shell too

You get an audit trail and the option to reverse it. See lock and unlock a user account for the full set. Delete only once you are sure nothing depends on the account.

Remove the group too, if it was theirs

userdel removes the user's primary group if no one else uses it, but a shared group it merely belonged to stays. To remove a now-empty group:

bash
sudo groupdel developers

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

TagsLinuxuserdelUser ManagementSystem AdministrationSecurity

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts