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.
If you encounter below error while running the userdel command:
userdel: command not found
you may try installing the passwd package as shown below as per your choice of distribution.
Distribution | Command |
---|---|
Debian | apt-get install passwd |
Ubuntu | apt-get install passwd |
Alpine | apk add shadow |
Arch Linux | pacman -S shadow |
Kali Linux | apt-get install passwd |
Fedora | dnf install shadow-utils-2 |
Raspbian | apt-get install passwd |
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