How to Remove a User and Home Directory in Linux [2 Methods]

In the multiuser Linux operating system, having multiple users is a common scenario. However, at times, it may be necessary to remove a user account and its associated home directory, when that user is inactive or no longer needed. As it assures the security & cleanliness of the system. In this article, I will different ways you can remove a user along with its home directory with some practical examples in Linux. So let’s get started.

Key Takeaways

  • Learning to remove a user along with its home directory using both terminal & GUI.
  • Getting familiar with all the cases that can be used while using CLI to remove a user with its main directory.

Requirements

Process Flow Chart

Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTSProcess flowchart to remove a user and home directory in Linux

Watch 2 Methods to Remove a User and Home Directory in Linux

2 Methods to Remove a User and Home Directory in Linux

In Linux, you can easily remove a user account from your system using the userdel or deluser command. However, if you don’t specify the –remove option, or configure the /etc/deluser.conf file to remove the home directory whenever you are removing a user, it won’t simply remove the home directory or any user contents.

In the following article, I will discuss how you can remove a user along with its home directory using both Command Line Interface (CLI) & Graphical User Interface (GUI).

Anyway, you can read our Comparative Analysis of Methods to distinguish between these two methods and best pick one for your need.

Method 01: Using Terminal Remove User and Home Directory in Linux

You can easily & quickly remove any user account and all of its files using the Command Line Interface (CLI). Since several commands can be used to remove a user and its primary directory, I will discuss them in different cases in the following sections.

Moreover, I will also explain how you can edit the /etc/deluser.conf file to remove the home directory whenever you are removing a user.

Case 01: Using the userdel Command Remove User and Home Directory in Linux

The first command I will use to remove a user is the userdel command. It is a simple command tool that you can use with ease. Just type the command followed by the –remove or -r option of the command to remove the user’s home directory & then the user name. Follow the below steps to see practically:

Steps to Follow >

➊ First, open your Ubuntu terminal application.

➋ Then, type the following command in your command prompt to view all the users list on your system:

getent passwd {1000..60000}
EXPLANATION
  • getent: retrieves entries from a specified database.
  • passwd: password database.
  • {1000..60000}: User IDs ranging from 1000 to 60000.

➌ Press ENTER.List of all users present in the system The command lists all the users available on the system, along with their UID, GID, Full name & home directory location path. As you can see, I have users munny, john, hawaimithai, lilly_bee, winnie, and sam in my system.

➍ Afterward, type the following command to remove a user & its home directory:

sudo userdel -r sam
EXPLANATION
  • sudo: Grants administrative privileges.
  • userdel: Removes user accounts and associated files & directories.
  • –remove or -r: Command option to remove.
  • sam: One of the system user accounts.

➎ Finally press the ENTER key.using the userdel command, remove a user and home directory in Linux The command removes user sam, it’s home directory, all of its files also the mail spool.

Read More: 2 Cases of Recursive Remove of Directory in Linux

Case 02: Using the deluser Command Remove User and Home Directory in Linux

Another command that you can use to remove a user account, is the deluser command. With the command option –remove-home you can delete a user and its home directory. Go through the below steps to check practically:

Steps to Follow >

➊ First, open your Ubuntu terminal application.

➋ Then, type the following command to remove a user & its home directory:

Command Syntax >

sudo deluser options user_name
sudo deluser --remove-home sam
EXPLANATION
  • sudo: Grants administrative privileges.
  • deluser: Removes system user accounts.
  • –remove-home: Command option to remove home directory.
  • sam: One of the system user accounts.

➌ Now, Press ENTER.remove a user and home directory in Linux using the deluser command The command removes the user ‘sam’ and its assigned home directory from the system.

Read More: How to Find and Delete Directory in Linux [3 Methods]

Case 03: Remove the Currently Logged-in User After Killing All its Running Processes

You may find an error message while trying to remove a user that says ‘User X is currently being used by process XXXX’. It happens when you are trying to remove a user and the user is being used. In most cases, it happens when you try to remove a currently logged-in user. Check out the below steps to see how you can remove a logged-in user after killing all its running processes with the killall command:

Warning: DO NOT try this case with your main user account. This is because the main user account usually has administrative privileges, removing it can potentially lock you out of the system. Rather, try it on another user. Also, you can create a test user & apply the method to it.

Steps to Follow >

➊ Start by opening your Ubuntu terminal application.

➋ Then, type the following command to remove a user & its home directory:

sudo userdel -r sam
EXPLANATION
  • sudo: Grants administrative privileges.
  • userdel: Removes user accounts and associated files & directories.
  • –remove or -r: Command option to remove.
  • sam: One of the system user accounts.

➌ Press ENTER & type the following command to kill all the running processes that are using the user sam:

sudo killall -u sam
EXPLANATION
  • killall: Terminates all the processes.
  • -u: kills processes the specified user owns.

➍ Tap ENTER again & write the below command to force remove the user:

sudo userdel -f sam
EXPLANATION
  • userdel: Removes user accounts and associated files & directories.
  • –force or -f: Command option to force remove.

➎ Finally, press the ENTER key again.remove a logged-in user and home directory in Linux

OUTPUT ANALYSIS
  • The first command tries to remove the logged-in user sam while the user is being used by process 6126.
  • The second command is used to kill all the running processes that are using user sam. The command terminates the current session & kills the shell.
  • The third command is used to forcibly remove the user and then it logs out from the sam

Read More: How to Force Remove Directory in Linux? [Step-by-Step]

Case 04: Remove User and Home Directory By Editing /etc/deluser.conf File in Linux

Generally, when you are deleting a user with the deluser command without mentioning the –remove-home option, it doesn’t remove the user’s home directory. However, you can manually edit the /etc/deluser.conf file to remove the home directory every time you delete a user. Also, after that, you don’t need to add a command option anymore. Go through the below steps to perform:

Warning: Be cautious while editing the system file /etc/deluser.conf. As unnecessary changes may cause system errors.

Steps to Follow >

➊ At first, open your Ubuntu terminal.

➋ Next, type the following command in the command prompt to open the /etc/deluser.conf file in the nano editor:

nano /etc/deluser.conf
EXPLANATION
  • nano: Text editor for Unix-like
  • /etc/deluser.conf: Contains configuration files of the deluser command.
Open /etc/deluser.conf file in the nano editor➌ Press the ENTER button.

➍ Afterward, change the ‘REMOVE_HOME’ option value from “0” to “1”. Save the changes by pressing CTRL+S & exit by CTRL+X.Configure /etc/deluser.conf file in the nano editor to remove home directory After saving the changes, the deluser command will remove the home directory too whenever it will be used to remove a user account. You won’t need to mention the –remove-home command option anymore. Anyway, remember to change the value again when you don’t want to remove the home directory.

Method 02: Using GUI to Remove User and Home Directory in Linux

When you are not sure which command to use or getting unexpected errors while using CLI to remove a user with the home directory, you can always use the Graphical User Interface (GUI). Even though it could be a little more time-consuming, it is handy for beginners without CLI knowledge.

Steps to Follow >

➊ At first, go to settings. Scroll to the bottom & select the ‘Users’ option. In there, on the top right corner, you will find the ‘Unlock’ option. You have to unlock the section by giving the current user password for security purposes.Unlock to make changes in the users list ➋ Then, type the currently logged-in user password and authenticate.Authenticate to unlock ➌ Afterward, select the user you want to remove from the users’ list.Select user to remove using GUI ➍ Subsequently, select the ‘Remove User’ option, which you will find at the bottom. Then select the ‘Delete files’ to remove the user & also its corresponding home directory.remove a user and home directory in Linux using GUI With these few simple steps, you can delete any user and its associated files from your system using GUI. Moreover, if you want, you can also remove the user excluding the home directory, mail spool, and temporary files.

Read More: How to Remove a Non-Empty Directory in Linux [2 Methods]

Comparative Analysis of Methods

Since this article introduces multiple cases of methods for a single task, you might get confused about which one to pick. Therefore, in this section, I am presenting a comparative analysis of the methods for your convenience. You can learn the advantages and disadvantages of the available methods and choose which is the best for you.

Methods Pros Cons
Userdel Command
  • Simple command to remove a user account and files.
  • Removes the home directory & all user-associated files while the command option –remove is used.
Deluser Command
  • A userdel-like command with additional functionality.
  • Need to specify separate command options for removing all files & home directory.
Killall  & deluser Command
  • Killall terminates current processes and then deluser force removes the user with the –force option.
  • Trying to remove the main account while it is in use, may lock you out of the system.
/etc/deluser.conf
  • No need to use any additional deluser command option to remove the home directory after editing the configuration file.
  • Accidental removal of the home directory may happen when you don’t want to remove the directory as you forgot to modify the configuration file again.
GUI
  • User-friendly & easy to use as it does not require any knowledge of commands.
  • Slower due to clicking and navigation.

Each & every method has its pros & cons. You can choose any one of them depending on your expertise in that particular command. Anyway, if I had to then I would suggest using method 2, where I used the deluser command. Cause it is the simplest & safest method among them all.

Complementary Analysis

Besides, knowing about how to remove a user & with the home directory, you will find the below information helpful.

Check If the User & Home Directory Has Been Deleted in Linux

After removing a user and its home directory, you can check the completion of the removal using two simple commands. Follow the below steps for the practical checkout:

Steps to Follow >

➊ First, open the terminal.

➋ Then, type the below command to check if the user has been removed:

id sam
EXPLANATION
  • id: Finds the identity of the specified USER.
  • sam: One of the system user accounts.

➌ Press the ENTER key.Verify if the user has been removed The command output says “‘sam’: no such user”, indicating user sam has been removed.

➍ Afterward, type the below command to see if the user’s home directory has been removed:

sudo ls -l /home/sam
EXPLANATION
  • sudo: grants administrative privileges.
  • ls: Lists directory contents
  • -l –long: Option of the ls command where -l gives long lists.
  • /home/sam: Home directory location path of user sam.

➎ Finally press the ENTER key.Verify if the user's home directory has been removed. The command output says “‘/home/sam’: No such file or directory”, indicating the home directory has been removed.

Conclusion

To sum up, removing a user and its home directory is a common task in Linux system administration. In this article, I discussed the process using several practical examples. I showed how you can do that using both the command-line & graphical interface. Hope this article helps you to get a clear concept of ‘how to remove a user & its home directory’.

People Also Ask

How do I remove a home directory in Linux?
You can remove a home directory in Linux using the rm command with the –remove or -r command option. Moreover, you need to be a root user or have sudo privileges to modify any user. Hence, use the command syntax: “sudo rm – /home/user_name”.

How do I delete a home directory after deleting a user?
You can delete a user and its home directory using the ‘deluser –remove-home user_name’ command syntax. Anyway, if you have already removed the user & not the home directory then use the rm command with the –remove option. Hence, use the command syntax: “sudo rm – /home/user_name”.

What command is used to delete the user Linux with his home directory?
In Linux, generally, the command used to delete a user with its home directory is the userdel command with the –remove option. Hence, use the command syntax: “userdel -r user_name”.

Where is the user home directory in Linux?
When you create a user, the system creates a home directory by default for that user. And the directory is located typically at the ‘/home/username’ location. This directory is used to store the user’s personal files, documents, and configuration files.

Related Articles

5/5 - (5 votes)
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

2 thoughts on “How to Remove a User and Home Directory in Linux [2 Methods]”

Leave a Comment