Using Linux Userdel Command to Remove Users

Updated: Jun 8

Userdel command can be used to delete a user and related files in Linux. We are going through the whole process to check what will happen after we run userdel command.

Understanding Userdel Command

The following two steps will happen for userdel command.

  1. deletes the group with the same name as the user if USERGROUPS_ENAB is set to yes in this file /etc/login.defs file. It happens only if no other user is a member of this group. If this group belongs to a shared primary user group, it will not be deleted.

  2. removes the user entries from the /etc/passwd and /etc/shadow files

Which Directory will be Removed for Userdel Command?

In most Linux distributions, by default the user home and mail spool directories are not removed when removing a user account with userdel. If we are sure to remove these files, we can use -r option (--remove).

 -r, --remove
 Files in the user's home directory will be removed along with the home directory itself and the user's mail spool. Files located in other file systems will have to be searched for and deleted manually.

The files under other file systems are not deleted automatically. We need to remove these files separately. If the user is still login the system, userdel command will fail. We need to logout this user first.

Risky Option For Userdel Command

There is one risky option -f for this command. We should be very careful when we run it.

-f, --force
Remove the user even if they are currently logged in. Remove home directory and mail spool even if they are used by another user. Remove group too if USERGROUPS_ENAB is set to yes in /etc/login.defs.

Related Post:

Basic Linux Commands: Linux User Management

How useradd works in Linux

Linux Command: Change User Login Shell Safely

How to fix "WARNING: Your password has expired." in Linux?


Join our newsletter! Get a free cloud server!  Never miss a post!

Thanks for submitting!