How to Remove User in Linux Using the Command Line

This article will teach you how to remove a user from a Linux system. It covers the userdel command and its options.

As you know, Linux is a multi-user system, which means that more than one user can interact with the same system simultaneously. Therefore, user management is an essential part of Linux administration.

You’re probably already familiar with adding new users in Linux. So let’s now learn how to remove them.

userdel Command Syntax

The userdel command in Linux is a low-level utility used to remove a user account and related files. The command modifies the system account files, deleting all entries that refer to USERNAME.

userdel [OPTIONS] USERNAMECode language: CSS (css)

The command has only a few options, the most significant three of which are listed below:

OptionExplanation
-rFiles in the user’s home directory will be removed along with the home directory and the user’s mail spool. Files owned by the user in other file systems will have to be searched for and deleted manually.
-fForces the removal of the user account, even if the user is still logged in. It also forces userdel to remove the user’s home directory and mail spool, even if another user uses the same home directory or if the specified user does not own the mail spool.
-ZRemove any SELinux user mapping for the user’s login.

The exact syntax applies to any Linux distribution. To remove users using the userdel command in Linux, you need to be logged in as root or as a user with sudo privileges.

Remove User in Linux

For example, if you want to remove a user “john” from your Linux system, enter:

sudo userdel john

Remember that the userdel command won’t work if the user is logged in or has processes running under the account. Instead, you will get a message similar to the one displayed below.

Error when trying to delete a currently logged-in user
Error when trying to delete a currently logged-in user

In this case, you must use the -f option to force deletion.

sudo userdel -f john

Remove User along with Their Home Directory in Linux

When you run a userdel command without any options specified in the command line, the user’s home directory and mail spool will remain in the system.

Remove user in Linux
Remove a user in Linux

To remove the user “john” along with its home directory and mail spool, execute the userdel command with the -r option.

sudo userdel -r john
Remove user along with their home directory in Linux
Remove the user along with their home directory

The command will remove files in the user’s home directory, the home directory, and the user’s mail spool. Please note that the user’s files in other filesystems will have to be searched for and deleted manually.

Find Files Left by the User After Deletion

However, even if you use the -r option to delete a user in Linux, there may be leftover files in the file system that no longer have an owner. Therefore, it is best practice not to leave files that do not have an owner or group assigned to them.

You will need the user’s UID and GID to find files owned by them. Therefore, before running the userdel command, double-check that you have written the user’s UID and GID somewhere on the side. You can easily find them using the id command followed by the user name.

id john
Find user's UID and GID
Find the user’s UID and GID

Once you know the user’s UID and GID, you can delete the user. Then, use the find command to search the entire file system for any leftover files owned by the removed user.

sudo find / -uid 1001 -gid 1001 -exec ls -l {} \;
Find files left by the user after deletion
Find files left by the user after deletion

For more detailed information about the userdel command, you can head to the command’s man page.

Conclusion

This guide shows you how to remove a user from a Linux system using the userdel command. We also showed you how to find and delete any leftover files still owned by the deleted user.

We hope we were of help to you. Any feedback and suggestions are very welcome in the comments section below.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *