The fastest way to remove a user in Linux

To delete a user account in Linux, use the userdel command in the terminal. The basic syntax is userdel username, where you replace "username" with the actual account name. This removes the user from the system's password file and group memberships, but leaves their home directory and files intact.

If you want to delete the user and their home directory at the same time, add the -r flag: userdel -r username. This is the most common approach when you want to completely remove all traces of an account. You must have root or sudo privileges to run this command.

Before deleting a user, check what processes or files belong to them. Run ps aux | grep username to see if they have active sessions, and find / -user username to locate their files across the system. Killing active processes first prevents errors during deletion.

Key Takeaways

  • Use userdel username to remove a user account while keeping their home directory, or userdel -r username to delete the account and all their files at once.
  • You need root or sudo access to delete any user account, which means running the command with sudo or as the root user.
  • Check for active processes and open files owned by the user before deletion to avoid errors or data loss.
  • Deleting a user from a group without removing the account entirely uses gpasswd -d username groupname instead.
  • Some systems use deluser (on Debian-based distributions) as an alternative to userdel with a simpler syntax.

When to keep the home directory and when to remove it

Keeping the home directory is useful when you want to preserve a user's files for backup, archival, or legal reasons. The account is gone, but the data remains on disk under /home/username (or wherever their home directory was located). This approach takes less time and carries less risk of accidental data loss.

Removing the home directory makes sense when you are decommissioning an account completely — for example, when an employee leaves and you have already backed up their files, or when you are cleaning up test accounts. The -r flag deletes the home directory and all its contents, including subdirectories, mail spools, and any other files stored there.

If you delete the account without the -r flag and later decide you need to remove the home directory, you can do so manually with rm -rf /home/username. Be extremely careful with this command — the -rf flags force recursive deletion without prompting, so verify the path twice before pressing Enter.

Checking what belongs to a user before deletion

Run id username to see the user's ID number (UID) and group memberships. This tells you which groups the account belongs to and confirms the account exists before you attempt deletion.

Use ps aux | grep username to list any running processes owned by that user. If processes are still running, the deletion may fail or leave orphaned processes. Kill these processes first with kill -9 PID, where PID is the process ID shown in the output.

Search for files owned by the user across the entire filesystem with find / -user username 2>/dev/null. The 2>/dev/null part suppresses error messages from directories you do not have permission to read. This shows you what data exists outside the home directory — for instance, files in /tmp, /var, or other locations — so you can decide whether to back them up before deletion.

Deleting a user from a single group without removing the account

If you only want to remove a user from a group (not delete the entire account), use gpasswd -d username groupname. This removes the user from that group while keeping the account active and all other group memberships intact.

Check a user's current groups with groups username. This shows all groups the account belongs to. Every user also has a primary group (usually matching their username) and may belong to additional groups like sudo, docker, or wheel depending on what permissions they need.

Removing a user from the sudo group is a common task — it revokes their ability to run commands with elevated privileges. After running gpasswd -d username sudo, the user can no longer use sudo, though their account still exists and they can still log in.

Differences between userdel and deluser

Most Linux distributions use userdel, which is part of the shadow-utils package. Debian-based systems (Ubuntu, Linux Mint, Debian itself) also include deluser, a wrapper script that makes user deletion simpler and more interactive.

deluser username works similarly to userdel username, but deluser prompts you to confirm before deleting the home directory, reducing the risk of accidental data loss. The command deluser --remove-home username is equivalent to userdel -r username. On Debian systems, deluser is often preferred because it handles edge cases more gracefully.

If you are on a Red Hat, CentOS, Fedora, or other non-Debian system, userdel is your only option. Both commands do the same job — the choice depends on which distribution you are using.

What happens to files owned by a deleted user

When you delete a user without the -r flag, their files remain on disk but become orphaned — they are owned by a UID that no longer corresponds to any user account. If you run ls -l in the home directory, you will see the UID number instead of a username, like 1001 instead of john.

These orphaned files take up disk space and can create security issues if a new user is created with the same UID later — they would suddenly own files they should not have access to. To prevent this, either delete the home directory with userdel -r, or manually change ownership of important files to another user with chown newuser:newgroup /path/to/file.

If you delete a user and later create a new user with the same name, the new account will have a different UID and will not automatically own the old files. The old files remain orphaned until you manually reassign them or delete them.

Frequently Asked Questions

What happens if I try to delete a user who is currently logged in?

The deletion usually succeeds, but the logged-in user may experience errors when trying to access files or run commands. Their session becomes unstable because the system can no longer resolve their UID to a username. It is best to ask the user to log out first, or use pkill -u username to terminate all their sessions before deletion.

Can I undo a user deletion?

No — once deleted, the account is gone. If you deleted the home directory with -r, the files are also gone unless you have a backup. If you only used userdel without -r, the home directory still exists but is orphaned. You can recreate the account with useradd username, but the new account will have a different UID and will not own the old files.

How do I delete multiple users at once?

You can loop through a list of usernames in the terminal. For example, for user in user1 user2 user3; do userdel -r $user; done deletes all three accounts and their home directories. Be very careful with loops — a mistake in the syntax could delete the wrong accounts.

What if the home directory is on a different partition or network drive?

The -r flag only deletes the home directory specified in the user's entry in /etc/passwd. If the home directory is mounted from a network location or a separate partition, userdel -r will still delete it, but you should verify the path first with grep username /etc/passwd to see exactly where it points.

Do I need to delete the user's cron jobs or mail spool?

The userdel -r command removes the mail spool (usually in /var/mail/username) and the home directory, but it does not always remove cron jobs stored in /var/spool/cron. Check for leftover cron entries with crontab -u username -l and remove them with crontab -u username -r if they still exist after deletion.