How to Delete/Remove Users in Linux (userdel Command)

Updated 

3 min read

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 have the responsibility to manage the system's users and groups by creating new users and assign them to different groups.

Occasionally, you might need to delete a user account. Perhaps because the user moved away from the organization, or it was created for a specific service that no longer runs on the system.

In Linux, you can delete a user account and all its associated files using the userdel command.

This tutorial covers the userdel command and its options.

userdel Command Syntax

The syntax for the userdel command is as follows:

userdel [OPTIONS] USERNAME

To delete users using the userdel command, you need to be logged in as root or a user with sudo access.

How to Delete User in Linux

To delete a user account named username using the userdel command you would run:

userdel username

When invoked, the command reads the content of the /etc/login.defs file. Properties defined in this file override the default behavior of userdel. If USERGROUPS_ENAB is set to yes in this file, userdel deletes the group with the same name as the user, only if no other user is a member of this group.

The command removes the user entries from the /etc/passwd and /etc/shadow, files.

In most Linux distributions, when removing a user account with userdel, the user home and mail spool directories are not removed.

Use the -r (--remove) option to force userdel to remove the user's home directory and mail spool:

userdel -r username

The command above does not remove the user files located in other file systems. You have to search for and delete the files manually.

If the user you want to remove is still logged in, or if there are running processes that belong to this user, the userdel command does not allow to remove the user.

In this situation, it is recommended to log out the user and kill all user's running processes with the killall command:

sudo killall -u username

Once done, you can remove the user.

Another option is to use the -f (--force) option that tells userdel to forcefully remove the user account, even if the user is still logged in or if there are running processes that belong to the user.

userdel -f username

Conclusion

In this tutorial, you learned how to delete user accounts in Linux using the userdel command. The same syntax applies for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux.

userdel is a low-level utility, Debian and Ubuntu users will more likely use the friendlier deluser command instead.

Feel free to leave a comment if you have any questions.