As the Ubuntu distribution offers a multi-user system, being able to delete a user account is considered to be as essential as creating one. One may feel the need to remove an existing user account along will all of its files. Ubuntu provides approaches from both CLI(Command Line Interface) and GUI(Graphical User Interface) to delete a user and all files of it. In this article, I will demonstrate the possible methods to delete a user account and all its files in Ubuntu.
Process flow chart to delete user and all files in Ubuntu:
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
3 Methods to Delete User and All Files in Ubuntu
There is more than one command that can be used from CLI to delete a Ubuntu user and its files. You can do the same from Ubuntu’s GUI as well. However, while running commands in CLI you can choose if you want to handle the user and the files individually. Depending on such cases, I have divided the ways to achieve this task into three different Methods.
However, to remove a user in Ubuntu, you must either be the root user or have root/sudo privileges.
Method 1: Using a Command to Delete User and Files at once
You can easily remove a user and all of its files using the deluser command or, userdel command in Linux. Generally, the home directory created under the username contains all the files owned by that user. The deluser
command applied with the --remove-home
option deletes a user along with its home directory. Therefore removes a user and all files.
In this example, I will delete the user account “user2” and its home directory from my system. To do the same follow the below procedures:
- First, open the Ubuntu terminal.
- Type the following command in the command line:
sudo deluser --remove-home user2
EXPLANATION- sudo: Grants administrative privileges.
- deluser: Deletes a user account.
- –remove-home: Removes home directory and its contents.
- user2: Username of the user to be deleted.
- Press ENTER.
- Give the password of the currently logged-in user and hit ENTER. As you can see in the above image, I have deleted the user account “user2” along with its files using a single command. The ls command in Linux can be utilized to verify the deletion of the user’s files.Furthermore, you can check if the user is actually deleted using the getent command.
Method 2: Using Two Commands to Delete User and Files Separately
You can choose to delete a user account and its file separately. In this case, at first, you will need to delete the files available under the desired username and then delete the desired user.
Since the home directory of a user indicates all the files of the user, I will demonstrate deleting the home directory under a specified username using the find command. After the successful deletion of the files, I will delete only the user with the
userdel
command. You can do the same by following the process below:- Open the Ubuntu terminal.
- Then, type the following command in the command prompt and press the ENTER button.
sudo find / -user user2 -delete
EXPLANATION- sudo: Grants administrative privileges.
- find /: Searches for files and directories from the root directory.
- -user: Specifies to find files and directories owned by the desired user.
- user2: name of the desired user.
- -delete: Deletes the files and directories.
- Then, give the password of the current user and hit ENTER.
- Additionally, check the home directory if it is deleted with the command below:
ls /home
EXPLANATION- ls /home: Lists the files and directories under the specified path.
- Finally, remove the desired user with the following command:
sudo userdel user2
EXPLANATION- sudo: Grants administrative privileges.
- userdel: Deletes a user account.
- User2: Username of the user to be deleted.
In the above image, you can see that, I have deleted the user and the files individually. The files are removed from the system before the user account deletion.
Upon deletion, you can check the deleted user in Ubuntu from this section.
- How to Delete User Account In Ubuntu? [2 Methods]
- Delete a User from an Ubuntu Server in Just 3 Steps
Method 3: Using “Users Settings” in GUI to Delete User and Files
If you do not have any knowledge of the commands in Linux, you can remove a user and the files from the GUI. To perform this task you must log in as a root user or, you must have the same administrative privileges as a root user. If you have such permissions, then you can follow the given steps to delete a user from your system using GUI:
- At first, click the Applications menu and then select “Settings” from the available applications.
- Go to the “Users” settings as shown in the image below.
- Press the Unlock button to perform actions as administrator.
- Authenticate your user account by entering the password for the currently logged-in user.
- SELECT the desired user account to be deleted and then CLICK the Remove user button. In this example, I will delete the user “new_user2” by clicking the Remove user button under this user.
- Now, a dialog box will appear on your screen giving multiple options regarding the deletion. You can either keep or delete the user’s home directory along with the user account. To completely remove the user SELECT the “Delete Files” option. Upon completion of the given steps, the desired user account will be removed from your system. You can modify the deletion or cancel the task from the last step.
Comparison of Methods to Delete User and All Files in Ubuntu
Since this article introduces multiple 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 three different approaches 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 Method 1 - Requires only a single command to complete the whole task.
- A much faster approach.
- Users cannot specify any special file location for removal.
Method 2 - Provides an opportunity to handle user and the files individually.
- If needed, users have the option to specify the desired file location.
- Requires two different commands to achieve the task.
Method 3 - Does not require any prior knowledge of the command line.
- A much more lengthy and time-consuming procedure.
Summing up it can be said that all three methods come with their pros and cons. Therefore, the suitable method varies from user to user. However, If you are comfortable with the CLI you can go for Method 1 or, Method 2. On the other hand, If you do not have any idea about the Linux commands, you can use the approach through GUI as in Method 3.
How to Check Deleted User in Ubuntu?
After completing the deletion of accounts, it is necessary to verify if the user is removed from the system. For this, you can use the getent command to look into the “passwd” file of the system where all the user information is stored. If some user has successfully deleted this file will not be any data under that name.
For example, I have deleted the user account “user2”. Now, in this section, I will check the deleted user in my Ubuntu system. You can do the same by following the steps below:
- First, open the Ubuntu terminal.
- Type the following command in the command line:
getent passwd | grep user2
- Press ENTER.As you can see in the above image, the “user2” does not exist. Therefore, the user has been successfully removed from the system.
- Remove User from a Group Using “gpasswd” Command in 3 Steps
- How to Delete Multiple Users in Linux? [2 Steps]
How to Delete User’s All Files in Linux?
If you have deleted a user without its all files or home directory. Or maybe if you just want to remove the home directory of a user, you can utilize the basic rm command in Linux. In this example, I will remove the home directory of “UserC”. Follow the instructions below to delete the user’s all files or home directory:
- First, open the Ubuntu terminal.
- Type the following command in the command line:
sudo rm -r /home/UserC
EXPLANATION- sudo: Grants administrative privileges.
- rm -r: Removes files and directories recursively.
- /home/UserC: Path to the directory to be deleted.
- Press ENTER.
- Give the password of the currently logged-in user and hit ENTER.
- Furthermore, to check if the directory is deleted type the command below:
ls /home
EXPLANATION- ls /home: Lists the files and directories under the specified path.
In the above image, you can see that I have deleted the home directory called “UserC” using the basic
rm
command. You can use thels
command to list the available home directories.
Conclusion
To conclude, this article illustrates different methods for the deletion of user accounts along with the user’s files. I showed approaches using both CLI and GUI in Ubuntu. Moreover, the article demonstrates the deletion of the home directory even without removing the user. Users of beginner to advanced levels can use the explained methods to achieve this goal. Hence, following this guide will aid their journey in Linux.
People Also Ask
Related Articles
- 3 Ways to Delete the User Password in Ubuntu
- How to Delete MySQL User in Ubuntu? [2 Cases]
- How to Remove A User from Group in Linux [4 Methods]
LINUX
FUNDAMENTALS A Complete Guide for Beginners