In Linux, we can have multiple user accounts. Sometimes, we may need to delete some users to deny their access to the system. In such cases, we have to delete the user account. We can delete a user account in Linux using the
userdel command-line utility.
userdel [options] user
It removes the user with the username
user from the system, and
options are various options associated with the
userdel command to customize the deletion process. To delete a user account, we must make sure we are logged in as a superuser.
It deletes the account with the username
DelftStack. This command also reads content of
/etc/login.defs file. If the group with the name
DelftStack also exists and if the user
DelftStack is only present in the
DelftStack group, the
DelftStack group also gets deleted if
USERGROUPS_ENAB is set
yes in the
The command also clears all information about the user present in
/etc/shadow files also.
However, some directories like mail spool and the user home are not deleted even after deleting the account using the
To delete the directories, we use the
--remove option with the
userdel -r DelftStack
It deletes the mail spool and user home directories. However, the files in other file systems must be deleted manually.
We cannot delete the user account if the user is logged in or any processes associated with the user are running in our system. To logout the user from the system and stop all the processes associated with the user, we use the following command:
sudo killall -u DelftStack
It logs out the user
DelftStack of the system and stops all the processes associated with the user
Now, we can delete user account using the command:
Alternatively, to delete the user account logged in or the user who has any running processes associated with it, we can use use the
--force option with the
userdel -f DelftStack
It forcefully deletes removes the account
DelftStack from the system even if the user is logged in or the user has any running processes associated with it.