Linux delete user command

I am a new Linux sysadmin. How do I remove a user account named foo? How can I delete user using the command line option?

Do you want to remove a user account from the Linux based system? Use the userdel command on the command line. This page shows how to delete users on Linux.

Linux delete user command

As root user type command
userdel {username}
sudo userdel {username}
userdel [option] {username}

How to list users on Linux system

Use the cat command or more command to view /etc/passwd file:
$ cat /etc/passwd
$ more /etc/passwd

How to search for given username

Use the following grep command to locate a particular user name:
$ grep "^username" /etc/passwd
$ grep "^raj" /etc/passwd

Deleting user account named raj

Say you want to delete user named raj, run:
$ sudo userdel raj
OR
# userdel raj
Verify that user account deleted from the Linux:
$ grep "^raj" /etc/passwd

Deleting user account named raj using userdel command on Linux

Linux remove user account command

One can combine usedel command with the -r option. The -r option removes user’s home directory along with all files and email. The syntax is:
userdel -r {username}
sudo userdel -r {username}

For example remove user named tristan and delete the user’s home directory too:
$ sudo userdel -r tristan
OR
# userdel -r tristan
Make sure user account delete on Linux. Run id command or grep command for verification:
$ id tristan
$ grep "^tristan" /etc/passwd
$ sudo ls /home/tristan/

How to remove any SELinux user mapping for the user’s login

Try:
sudo userdel -Z -r {username}
sudo userdel -Z -r vivek

How to forcefully delete user account when is currently logged in to Linux server

Pass the -f option to the userdel command. It forces the removal of the user account, even if the user is still logged in. For example:
$ sudo userdel -r rob
[sudo] password for vivek:
userdel: user rob is currently used by process 4650

To get rid of “userdel: user rob is currently used by process 4650“, run:
$ sudo userdel -r -f rob

Conclusion

Use userdel command to delete a user account and related files on Linux.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.