How to Delete User Accounts and Their Home Directories in Linux

As a System Administrator in Linux, you may have to remove a user’s account after some time when a user account may become dormant for so long, or the user may leave the organization or company for any other reasons.

When removing user accounts on a Linux system, it is also important to remove their home directory to free up space on the storage devices for new system users or other services.

In this tutorial, I am going to take you through steps you can use to delete a user’s account together with his/her home directory on a Linux system.

To learn how to create user accounts and manage them on Linux systems, read the following articles from the links below:

How to Delete/Remove a User Account with Home Directory

For demonstration purposes, first I will start by creating two user accounts on my system that is user tecmint and user linuxsay with their home directories /home/tecmint and /home/linusay respectively using the adduser command.

adduser tecmint
passwd tecmint

adduser linuxsay
passwd linuxsay
Create New User Accounts in Linux
Create New User Accounts in Linux

From the screenshot above, I have used the adduser command to create user accounts on Linux. You can also use the useradd command, both are the same but the main difference between these commands lies in their intended usage and user interface.

The “adduser” command is a higher-level interface for adding users to a system, performs any necessary setup, and creates a home directory for the user by using default settings.

On the other hand, “useradd” is a lower-level utility for adding users to a system, and updating user information. It does not create the user’s home directory by default and requires the administrator to specify additional options manually.

Let’s now move further to see how to delete or remove user accounts in Linux using deluser (For Debian and its derivatives) and userdel (For RedHat-based systems) commands.

sudo deluser --remove-home username
sudo userdel -r username

The above command will delete the user’s home directory along with their account.

The directives inside the configuration file for deluser and userdel commands determine how it will handle all user files and directories when you run the command.

Let us look at the configuration file for the deluser command which is /etc/deluser.conf on Debian derivatives such as Ubuntu, Kali, and Mint, and for RHEL/CentOS/Fedora users, you can view the /etc/login.defs files.

The values in these configurations are default and can be changed as per your needs.

vi /etc/deluser.conf         [On Debian and its derivatives]
vi /etc/login.defs           [On RedHat/CentOS based systems]
Del User Configuration
Del User Configuration

To delete a user with a home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that users can only be deleted effectively when they are not logged on to the server.

How to Lock User Accounts in Linux

To lock a user account in Linux, you can use the passwd command followed by the -l or --lock option and the username. Locking a user account prevents the user from logging in while still retaining the account and its associated files.

sudo passwd -l username
OR
sudo passwd -lock username
Lock User Account Password in Linux
Lock User Account Password in Linux

To unlock the account, you can use the passwd command again with the -u option.

sudo passwd -u username

How to Find and Kill User Running Processes in Linux

You can find and kill user-running processes in Linux using the ps command to find the processes associated with a specific user and the kill command to terminate them.

Finding User Processes

Use the ps command with the -u flag followed by the username to list processes for a specific user.

ps -u username

Then you can list the processes in terms of username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, and command path in a full formatting style with the help of the following command as shown:

ps -f --pid $(pgrep -u tecmint)
Find All Running Processes of User
Find All Running Processes of the User

Killing User Processes

Once you find all the running processes of a user, you can use the killall command to kill those running processes as shown.

killall -9 -u tecmint

The -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines the username.

Note: In recent releases of RedHat/CentOS 7.x versions and Fedora 21+, you will get an error message:

-bash: killall: command not found

To fix such an error, you need to install psmisc package as shown:

yum install psmisc       [On RedHat/CentOS 7.x]
dnf install psmisc       [On Fedora 21+ versions]

How to Backup User Data Before Deleting

To back up user data before deleting a user account on a Linux system, you can follow these commands.

sudo mkdir /backup
sudo cp -r /home/username /backup

Optionally, you can create a compressed archive of the user’s data to conserve storage space. I have used the tar command to create a backup of user home directory as follows:

sudo tar -zcvf /backup/username_backup.tar.gz /backup/username
Backup User Home Directory in Linux
Backup User Home Directory in Linux

How to Delete/Remove User Files in Linux

Now you can safely remove the user together with his/her home directory, to remove all user files on the system use the --remove-all-files option in the command below:

deluser --remove-home tecmint      [On Debian and its derivatives]
userdel --remove tecmint           [On RedHat/CentOS based systems]
Delete User Account with Home Directory
Delete User Account with Home Directory
Summary

That is all to do with removing user and their home directory from a Linux system. I believe the guide is easy enough to follow, but you can voice a concern or add more ideas by leaving a comment.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

15 Comments

Leave a Reply
  1. You say that adduser and useradd is the same but the manpage for useradd says it is a low level utility for adding users and admins should use useradd instead. If you check the file sizes you will notice that useradd is about 4 times bigger than adduser.

    Reply
  2. In the ReHat version. How can I delete a user without deleting his/her home directory.
    We have multiple users that share the same home directory.
    Thanks

    Reply
  3. Tutorial is good, but one thing is missing: no word about corresponding group.

    Warning: group `tecmint’ has no more members.

    Reply
  4. In the last step you make reference to a –remove-all-files option. What other files besides the home directory would be removed with that option?

    Reply
    • @warren

      The user’s mail spool, however, – -remove-all-files option does not have any effect any more for that purpose. Because – -remove-home removes both the user’s home directory and mail spool. Additionally, the – -backup option backups all files contained in the userhome and the mailspool-file to a file named /$user.tar.bz2 or /$user.tar.gz on the system.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.