userdel Command Examples
If you want to delete an existing user in Linux, you can use the userdel command in the terminal.
This is a straightforward command with only a few options. Here’s its syntax:
userdel [options] username
Deleting users in Linux command line
First, list the users in your Linux system to get the username of the user you want to delete.
Now, to delete the user, all you have to do is to use the userdel command with the username like this:
sudo userdel user_name
Keep in mind that it won’t delete the user’s home directory by default. Of course, you can delete user directories on your own but userdel provides an easier option.
The next section shows how to delete user along with its home directory.
Remove user with home directory
You can use the option -r to remove a user along with home directory and mail spool (a file that stores the mail header and message body of the email account of the user).
sudo userdel -r user_name
Do note that it is not advisable to delete the user, specially with its files, in a large organization. You might need the user files.
If a person leaves your organization, instead of removing the user and its files, you can simply lock the user with usermod command.
Bonus Tip: Don’t use -f option with userdel command
If you visit the man page of userdel command, you’ll find -f option. The man page itself advise to not use this option.
This option forces the removal of the user account, even if the user is still logged in. It also forces userdel to remove the user’s home directory and mail spool, even if another user uses the same home directory or if the mail spool is not owned by the specified user.
If USERGROUPS_ENAB is defined to yes in /etc/login.defs and if a group exists with the same name as the deleted user, then this group will be removed, even if it is still the primary group of another user.
This option is dangerous and may leave your system in an inconsistent state.
That’s it…
You cannot delete multiple user simultaneously. You can only delete one user at a time.
As you can see, it’s really simple to use the userdel command to delete users in Linux. There are not too many options to confuse you.
I hope you like this quick little Linux learning. Your suggestion is welcome.