Userdel command can be used to delete a user and related files in Linux. We are going through the whole process to see what happens after we run userdel command.
The following two steps will happen for userdel command.
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.
removes the user entries from the /etc/passwd and /etc/shadow files
In most Linux distributions, 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.
These 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.
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.