How to Use userdel Command in Linux – Remove Users

Written by: Bobbin Zachariah   |   Last updated: February 17, 2024

A system administrator might require to remove a user when the user account is compromised, a user no longer required, or when the user itself creates problems.

The userdel command is a low-level utility that can be used to delete an existing user and associated files from the Linux system.

By default, userdel removes the user's entry from the /etc/passwd file, the /etc/shadow file (which contains the encrypted passwords), and the /etc/group file (if the user has any group memberships). It also removes the user's home directory (usually located in /home/username), along with any files it contains, if you use the -r option.


userdel [options] username

Where username is the name of the user to get removed. To run the command requires root or sudo access privileges.

These are common files that could be touched when deleting a user /etc/group, /etc/login.defs (on Debian based /etc/deluser.conf), /etc/passwd and /etc/shadow.

Remember if you using a Debian-based system the preferred command to delete user is deluser.

Keep in mind that deleting a user can have unintended consequences, so it is important to be careful when using this command.


The userdel command in Linux has two options when deleting a username.

  • -r - Remove the user’s home directory and mail spool along with the username.
  • -f - Forcefully remove the user account, even if the user is still logged in or if there are running processes that belong to the user.
  • -z : Remove the users SELinux user mapping.

Userdel Examples

Let's check how to use userdel command with few practical examples.

Removing a user

Type username followed by the userdel command to delete a user.

For example to delete a user named student1, type:

sudo userdel student1

This will delete the student1 user account, but will not remove the user's home directory or any files owned by the user. The command exit with zero output if successfully deleted the user account.

Deleting user including home directory

Use -r option to delete user including the user's home directory and mail spool.


sudo userdel -r student2

This will delete the student2 user's home directory and all files in it. You can verify using ls command to confirm home directory deleted.

Related Read: How to Delete a User with Home Directory on Ubuntu

Forcing delete

The -f option can be used with the userdel command to force the deletion of a user account, even if the user is still logged in or files cannot be removed. This usually becomes useful when the user is associated with a running process. You may also use kill -9 <pid>, followed by userdel command.

Forse user delete example:

sudo userdel -rf student3

This tells userdel to ignore any errors that may occur when deleting the user's home directory or files. This force deletes the home directory even if it's owned by another user.

Recommended steps when removing a user

Let's check the proper steps to safely remove a user in Linux.

Step 1. Prevent access by adding lock to user account

By executing the chage command with the -E flag we can set an expiration date for any user account. So if we set -E to 0 for any account, that account will instantly expire and the user will be locked out of the system.

sudo chage -E 0 bob

The passwd -l and usermod -L commands can also lock the user's account but the user can still authenticate using other methods such as SSH key authentication.

Step 2. Kill process used by user

After locking a user account, kill any processes that are running under their account.

Use the following pgrep to find all processes associated with the user:

sudo pgrep -u bob
ps -u username

This command displays the Process IDs of all the processes used by the user named bob.

You may now all processes associated with the user using the pkill command.

sudo pkill -u bob
kill pid-number

Step 3. Remove all schedules/cron jobs

Use the crontab command to list the cron jobs for the user you want to delete.

crontab -u bob -l

This will display a list of all the cron jobs that are scheduled to run under the bob user.

sudo crontab -r u bob

Now you can delete all the cron jobs for the user, you can use the -r option with the crontab command.

crontab -u bob -r

This will delete all the cron jobs for the bob user.

Step 4. Remove the user

If you've completely followed the above steps, we can now safely delete user accounts. This is done by executing the userdel command accompanied by the -r option.

sudo userdel -r bob

This will permanently delete the user and their files, so use caution when using this command.

Can userdel remove a group?

When a user is deleted using userdel, its primary group will also get removed if the group doesn't contain any other users. ie If the group contains a user other than the primary user, then user will be deleted and the group will stay.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment