How to Delete a User Account in Linux

Suraj Joshi Feb 02, 2024
  1. Syntax: userdel Command
  2. Example: userdel Command
How to Delete a User Account in Linux

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.

Syntax: userdel Command

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.

Example: userdel Command

userdel DelftStack

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 /etc/login.defs file.

The command also clears all information about the user present in /etc/passwd and /etc/shadow files also.

However, some directories like mail spool and the user home are not deleted even after deleting the account using the userdel command.

To delete the directories, we use the -r or --remove option with the userdel command.

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 DelftStack.

Now, we can delete user account using the command:

userdel DelftStack

Alternatively, to delete the user account logged in or the user who has any running processes associated with it, we can use use the -f or --force option with the userdel command.

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.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux User