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. To remove a user and their home directory in Linux, use sudo userdel -r username
. Here replace username
with the username of the user you want to remove.
Further, this will explore different ways to remove a user along with its home directory with some practical examples in Linux. So let’s get started.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
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.
1. Using Command-line Interface (CLI) 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.
Here are the ways to remove a user and home directory given below:
A. Using the userdel Command
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:
- 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}
-
Press ENTER. 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. 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
B. Using the deluser Command
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:
- First, open your Ubuntu terminal application.
- Then, type the following command to remove a user & its home directory:
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. 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]
C. Killing and Removing the Currently Logged-in User
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.
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:
- 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.
- 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]
D. 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 delete a user and home directory:
- 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.
- 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.
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.
2. Using Graphical User Interface (GUI)
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.
To remove user and home directory with GUI, follow the steps given below:
-
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.
- Then, type the currently logged-in user password and authenticate.
- Afterward, select the user you want to remove from the users’ list.
- 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.
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 |
|
|
Deluser Command |
|
|
Killall & deluser Command |
|
|
/etc/deluser.conf |
|
|
GUI |
|
|
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:
- 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. 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. 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?
To remove You can remove a home directory in Linux using the rm command with the –remove or -r command option. Moreover, you must 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?
To delete a home directory after deleting a user in Linux, you can use the rm command with the -r option, which stands for recursive deletion. Here’s how you do it: sudo rm -r /home/username
. Further, you can delete a user and its home directory using the deluser --remove-home user_name
command syntax.
What command is used to delete the user Linux with his home directory?
To delete a user in Linux along with their home directory, you can use the userdel command with the -r option. Here’s the command: 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. 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
- Remove All Files from Current Directory in Linux [2 Methods]
- 3 Ways to Remove Write Protected Regular File Using the rm command
- Remove All Files in a Directory with the Prompt in Linux
- How to Undelete Folder in Ubuntu? [With Solutions]
- 3 Ways to Remove All Files in a Linux Directory
FUNDAMENTALS A Complete Guide for Beginners
Well written
Thanks!