How to Remove a User in Linux

By 

•

Updated on

•

12 min read

Remove a user in Linux

When an account is no longer needed, you can remove it from Linux with the userdel command:

Terminal
sudo userdel username

This removes the user account but keeps the home directory and files. If you also want to delete the home directory and mail spool, back up anything you want to keep before running userdel -r.

Terminal
sudo userdel -r username

Linux is a multi-user system, which means that more than one person can interact with the same system at the same time. As a system administrator, you manage users and groups by creating new users , assigning them to groups , and removing accounts when someone leaves the organization or when an account is no longer needed.

The userdel command removes a user account from a Linux system. It deletes the user’s entries from the /etc/passwd and /etc/shadow files.

Only root or users with sudo privileges can delete user accounts.

Quick Reference

TaskCommand
Remove a user and keep home directorysudo userdel username
Remove a user and home directorysudo userdel -r username
Force remove a logged-in usersudo userdel -f username
Remove user and SELinux mappingsudo userdel -Z username
Send SIGTERM by effective user IDsudo pkill -u username
End active login sessionssudo loginctl terminate-user username
Remove user’s cron jobssudo crontab -r -u username
Stop user services at bootsudo loginctl disable-linger username
Disable an account temporarilysudo usermod -L --expiredate 1 username
Find files with no valid ownersudo find / -nouser
Find files by UIDsudo find / -uid 1001
Verify user was removedid username
Remove user with deluser on Debian/Ubuntusudo deluser --remove-home username

userdel Command Syntax

txt
userdel [OPTIONS] USERNAME

When invoked without options, userdel removes the user account but leaves the home directory and mail spool intact. The command also reads /etc/login.defs; if USERGROUPS_ENAB is set to yes, it deletes the user’s primary group only if no other users belong to it.

Alongside /etc/passwd and /etc/shadow, the command clears the account’s entries from /etc/group, /etc/gshadow, and the /etc/subuid and /etc/subgid files that map the subordinate ID ranges used by rootless Docker and Podman containers.

To work on a different root filesystem, such as a rescue system mounted at /mnt/sysimage, pass the -R (--root) option with an absolute path:

Terminal
sudo userdel -R /mnt/sysimage username

Before You Remove a User

Warning
Before you delete a user, back up any important data and make sure the account is not actively used.

Before removing a user, decide what should happen to the account’s files and scheduled tasks. A safe cleanup usually means checking these items first:

  • Back up files from the user’s home directory if they may be needed later.
  • Check whether the user is logged in or has running processes.
  • Remove the user’s cron jobs, at jobs, and systemd user services if the scheduled work should not continue.
  • Lock the account instead of deleting it if access only needs to be disabled temporarily.

Remove a User in Linux

To remove a user account and keep the home directory:

Terminal
sudo userdel username

This removes the user from /etc/passwd and /etc/shadow but keeps the home directory and all user files.

Remove a User and Home Directory

Warning
The -r option permanently deletes the user’s home directory and its contents. Back up any important data before running this command.

To remove the user account along with the home directory and mail spool, use the -r (--remove) option.

Terminal
sudo userdel -r username

If the user has no mail spool, you may see this message on Ubuntu and Debian.

output
userdel: username mail spool (/var/mail/username) not found

The message means there was no spool file to remove. Account creation settings determine whether useradd creates one. Check the exit status and any other messages to see whether the rest of the removal succeeded.

Files owned by the user in other locations are not removed. See Finding Orphaned Files below.

Remove a Logged-In User

If the user is still logged in or has running processes , userdel will refuse to remove the account.

Start by listing processes with the user’s effective UID.

Terminal
ps -u username

If those processes can be stopped, send them SIGTERM with pkill .

Terminal
sudo pkill -u username

killall accepts the same option and does the same job:

Terminal
sudo killall -u username

Of the two, pkill is the safer default on an unfamiliar server. It ships with procps-ng, which Debian, Ubuntu, RHEL, and Fedora all install by default, while killall comes from psmisc, a package that minimal RHEL and Fedora installations often leave out.

Once the processes have stopped, delete the account. Add -r only if you also want to delete the home directory and mail spool.

Terminal
sudo userdel username

Alternatively, the -f (--force) option removes the account and carries out any other requested actions while skipping the safety checks, including the check for an active login:

Terminal
sudo userdel -f username

Reach for -f only when you know what it bypasses. The manual page calls the option dangerous and warns that it may leave the system in an inconsistent state. Processes can keep running under a UID that no longer maps to an account. When -f is combined with -r, ownership and shared-directory safety checks no longer protect the home directory and mail spool. If USERGROUPS_ENAB is enabled, -f can also delete a group matching the username even when it is still the primary group of someone else.

Removing Scheduled Jobs and Lingering Services

Deleting an account does not reliably remove its scheduled-job files or systemd linger setting. Some schedulers skip jobs once the username no longer resolves, while queued jobs and already-running user services may behave differently. Clean up these items before deleting the account to avoid stale configuration or work continuing under an unmapped UID.

Start with the user’s cron jobs:

Terminal
sudo crontab -r -u username

Next, look for one-off jobs queued with at :

Terminal
sudo atq
output
14	Tue Aug 18 03:00:00 2026 a username

The first column is the job number and the last column is the owner. Remove the jobs belonging to the account by passing their numbers to atrm:

Terminal
sudo atrm 14

On systemd distributions, an account can have lingering enabled, which starts its user services at boot and keeps them running with no login session open. Turn that off:

Terminal
sudo loginctl disable-linger username

Finally, end any session the user still holds:

Terminal
sudo loginctl terminate-user username

Do all of this before running userdel, while the username still resolves to a UID.

Removing the SELinux User Mapping

On distributions that run SELinux, such as RHEL, Fedora, and their derivatives, a login can be mapped to an SELinux user. That mapping is stored outside /etc/passwd, so a plain userdel leaves it in place.

Check whether the account has one:

Terminal
sudo semanage login -l
output
Login Name           SELinux User         MLS/MCS Range        Service

__default__          unconfined_u         s0-s0:c0.c1023       *
root                 unconfined_u         s0-s0:c0.c1023       *
username             staff_u              s0-s0:c0.c1023       *

The username row shows that the login maps to the staff_u SELinux user. Use -Z (--selinux-user) to clear that mapping along with the account.

Terminal
sudo userdel -Z username

Use -rZ if you also want to delete the home directory and mail spool.

Skipping this step leaves a stale entry behind. Because the mapping is keyed on the login name, a brand new account created later under the same name inherits the old confinement.

Locking a User Instead of Deleting

If you only need to disable access temporarily, use usermod to lock the password and expire the account:

Terminal
sudo usermod -L --expiredate 1 username

The -L option locks password authentication, while --expiredate 1 disables the account for other login methods, including SSH key authentication. Account expiration does not end sessions that are already open. On a systemd distribution, terminate them separately:

Terminal
sudo loginctl terminate-user username

To restore access later, unlock the password and restore the previous account expiration date. If the account did not have an expiration date before it was disabled, clear the temporary value:

Terminal
sudo usermod -U --expiredate "" username

Finding Orphaned Files

After deleting a user, files they owned outside the home directory remain on the system. These files are now owned by the user’s old numeric UID.

To find all files with no valid owner, use find :

Terminal
sudo find / -nouser

To find files owned by a specific UID, for example 1001:

Terminal
sudo find / -uid 1001

You can then decide to delete , reassign, or archive these files.

Verifying the User Was Deleted

To confirm the user account no longer exists:

Terminal
id username
output
id: 'username': no such user

You can also check:

Terminal
getent passwd username

No output means the user does not exist.

deluser on Debian and Ubuntu

On Debian-based distributions (Debian, Ubuntu, Linux Mint), the deluser command is a higher-level wrapper around userdel. It reads its configuration from /etc/deluser.conf and handles additional cleanup.

To delete a user and their home directory with deluser:

Terminal
sudo deluser --remove-home username

To remove the user from a specific group without deleting the account:

Terminal
sudo deluser username groupname

Troubleshooting

userdel: user username is currently used by process 1234
The number at the end of the message is the PID of a process blocking removal. Start by checking that process’s command and user IDs.

Terminal
ps -p 1234 -o pid,ruid,euid,suid,args

ps -u and pkill -u match the effective UID. userdel also checks the real and saved UIDs, so the reported process can be missing from ps -u username.

Stop the process normally if possible. If it belongs to an open login session, you can end the user’s sessions on a systemd distribution.

Terminal
sudo loginctl terminate-user username

Once the process has exited, run userdel again. If it reports another PID, inspect that process too.

userdel: user ‘username’ does not exist
The account name is wrong or the user was already removed. Check the account database:

Terminal
getent passwd username

If the command prints no output, the user does not exist.

The home directory was not removed
userdel username removes only the account. Use -r when you want to remove the home directory and mail spool:

Terminal
sudo userdel -r username

If the account was already removed, you can manually review and delete the old home directory after confirming it is no longer needed.

userdel -r also leaves the home directory in place when it belongs to another user:

output
userdel: /home/username not owned by username, not removing

In this case, userdel removes the account, leaves the directory in place, and exits with status 12. Check the owner with ls -ld /home/username before deciding what to do with the directory.

userdel: group username not removed because it has other members.
Other accounts still use the group as a supplementary group. userdel removes the user but keeps the group. To list the group members, run:

Terminal
getent group username
output
username:x:1001:mark

The last field shows that mark still belongs to the group. Before deleting it, check whether its members still need the access it grants. The third field is the group ID, 1001 in this example. Use it to look for files owned by the group.

Terminal
sudo find / -gid 1001

If you are retiring the group, reassign any files that still need group access. Then remove the group with groupdel .

Terminal
sudo groupdel username

Files are still owned by the deleted user
Files outside the home directory are not removed by userdel -r. Find files without a valid owner:

Terminal
sudo find / -nouser

Review the results and decide whether to delete, archive, or reassign those files.

crontab reports the user as unknown
This message means crontab -u could not look up the account name.

output
crontab:  user `username' unknown

Check the spelling first. If you already deleted the account, look for a leftover crontab and check its numeric owner. On Ubuntu and Debian, check the following path.

Terminal
sudo ls -ln /var/spool/cron/crontabs/username

On RHEL and Fedora, check /var/spool/cron/username instead. If you confirm the file belongs to the deleted account, remove it using the path you checked.

Terminal
sudo rm -i /var/spool/cron/crontabs/username

When you call userdel from a script, test its exit status rather than matching on the message text, which varies between shadow-utils versions:

CodeMeaning
0Success
1Cannot update the password file
2Invalid command syntax
6The specified user does not exist
8The user still has running processes
10Cannot update the group file
12Home directory, mail spool, or other file cleanup failed
14Cannot update the SELinux user mapping
16Cannot update the subordinate UID file
18Cannot update the subordinate GID file

With code 12, the account has been removed, but part of the file cleanup failed. A mail spool error can occur even when the home directory was removed successfully. Read the error output and check the reported path before doing any manual cleanup.

Codes 14, 16, and 18 apply only when the corresponding SELinux or subordinate ID support is enabled in the installed shadow-utils build.

FAQ

Does userdel delete the home directory?
Not by default. Use userdel -r to remove the home directory and mail spool along with the account.

What happens to files owned by a deleted user?
Files outside the home directory remain on the system, owned by the user’s old numeric UID. Use find / -nouser to locate them.

What is the difference between userdel and deluser?
userdel is the low-level command available on all Linux distributions. deluser is a higher-level wrapper available on Debian-based systems that reads configuration from /etc/deluser.conf and provides additional options.

Can I delete a user that is currently logged in?
Not without the -f flag. Either log the user out and kill their processes first, or use userdel -f to force the removal.

Does deleting a user remove their cron jobs?
No. userdel does not reliably remove the user’s crontab, queued at jobs, or systemd linger setting. Some schedulers skip jobs after the username stops resolving, but the stored entries remain. Run sudo crontab -r -u username, clear queued jobs with atrm, and run sudo loginctl disable-linger username before deleting the account.

How do I undo a user deletion?
You cannot undo userdel. If you deleted the home directory with -r, the data is gone unless you have a backup. You would need to recreate the user and restore their files.

Conclusion

The userdel command removes user accounts from Linux. Use -r when you also want to remove the home directory, clear scheduled jobs and active sessions before deleting logged-in users, and run find / -nouser afterward to locate the files left behind. For the other half of the workflow, the useradd cheatsheet covers the options used when creating accounts.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page