userdel Command Examples in Linux

The userdel command is used to delete user accounts. By default, it does not delete the user’s home directory unless you use the -r option. Deleting the user account removes all references to it. You would have to recreate the account and re-add it to groups in order to resemble the original identity. Use caution before deleting a user account.

Syntax

The syntax of the userdel command is:

$ userdel [options] {user names}
  • userdel [username]: This removes the user entry from the /etc/passwd file, but leaves the user’s home directory and mail intact. This way of deleting a user account is not recommended.
  • userdel -r [username]: This removes the user and the user’s home directory, along with the user’s mail box. This will remove the files owned by the user along with the user’s account from his home directory.

userdel Command Examples

1. Remove a user:

# userdel username

2. Remove a user in other root directory:

# userdel --root path/to/other/root username

3. Remove a user along with the home directory and mail spool:

# userdel --remove username

Conclusion

To remove a user from your system, use the userdel command. Note that the user must be logged out of the system before you can remove him. If you use the userdel command without the -r, the user will be removed from the system but his or her files will be left behind. Use the -r to remove all traces of the user. See the man page for userdel of a list of all the command-line options.

Related Post