Deleting Linux user accounts is easy. Use the userdel command to delete that user’s entry from the /etc/passwd and /etc/shadow files.
The basic syntax of this command:
userdel [options] username
The userdel command only has 3 parameters:
Delete user files
The -r or –remove parameter tells the system to delete all the user’s emails, the files in the user’s home directory, and even the home directory itself.
Force deletion
It can force deleting an account while a user is logged using the -f or –force parameter in conjunction with -r. This option deletes all of the user’s files even if the home directory is also used by another user.
Help
The -h or –help option displays the userdel command option.
Examples:
if we want to keep the user files, we will omit the -r option:
# userdel username
if we want to delete both the files and the user’s home directory:
# userdel -r username
To check if any files belonging to the deleted user (for example, in the /tmp directory) remain, use the find command with the -uid or -user option (before deleting itself):
# find / -uid 1001
The result will be a list of files owned by UID 1001 (user name deleted). We navigate through the list and decide if we want to delete them all or just selectively. However, it is advisable to do something with the remaining files, not leave them in the system as they are: another user added and having the same UID as the deleted one can become the owner of these files and have access to sensitive information – so we delete them, or manually assign them to another owner.
We must also keep in mind that the Samba server has its own list of users. Therefore, it is also advisable to delete users from the Samba database:
#smbpasswd -x username