How to Delete User and All Files in Ubuntu [3 Methods]

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:

Flow chart of delete user and 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.

You can read our Comparative Analysis of Methods to distinguish between these three methods and pick one for your need.

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:

  1. First, open the Ubuntu terminal.
  2. 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.
  3. Press ENTER.

  4. Give the password of the currently logged-in user and hit ENTER.Deleting user and files with a single command. 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:

  1. Open the Ubuntu terminal.
  2. 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.
  3. Then, give the password of the current user and hit ENTER.
  4. 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.
  5. 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.

    Deleting user and files with two commands.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.


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:

  1.   At first, click the Applications menu and then select “Settings” from the available applications.Opening Settings in Ubuntu.
  2.   Go to the “Users” settings as shown in the image below.Navigating to Users menu in Ubuntu.
  3. Press the Unlock button to perform actions as administrator.Unlocking administrative privileges.
  4. Authenticate your user account by entering the password for the currently logged-in user.Authenticating root user account.
  5. 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.Selecting a user to delete.
  6. 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.Selecting all user files to delete with user. 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.Viewing users from GUI.

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:

  1. First, open the Ubuntu terminal.
  2. Type the following command in the command line:
    getent passwd | grep user2
    EXPLANATION
    • getent: Looks into the administrative database that is given as its argument.
    • passwd: Contains user information.
    • Pipe (|): Redirects the standard output.
    • grep: Looks for a specific pattern given to it as an argument.
    • user2: Username of the newly created user to be checked.
  3. Press ENTER.Verifying if the user is successfully deleted.As you can see in the above image, the “user2” does not exist. Therefore, the user has been successfully removed from the system.


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:

  1. First, open the Ubuntu terminal.
  2. 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.
  3. Press ENTER.
  4. Give the password of the currently logged-in user and hit ENTER.
  5. 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.

    Deleting user's home directory with rm command in Linux.In the above image, you can see that I have deleted the home directory called “UserC” using the basic rm command. You can use the ls 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

How do I delete all users and files in Ubuntu?
The Linux os does not offer any command to delete all users and files at once. However, you can delete users and files one by one using the userdel or, deluser command.
How do I delete all files owned by a user in Linux?
By default, all files of an owner reside in the user’s home directory. You can delete all these files by directly deleting the desired home directory with rm -r or find command.
How do I delete a user process in Linux?
To delete a user process in Linux at first, you will need to retrieve the process ID using the following command: ps -u USER_NAME. Then you can use the kill command along with the process ID to end the process.
How to delete files in cmd?
To delete files in cmd you can use the del command. The syntax is as follows: del FILE_NAME.
How to delete folder Linux?
For removing a folder in Linux along with its subdirectories and files, you can use the rm command with the option -r. This option recursively deletes all files and directories within the folder.

Related Articles

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment