How to Delete User Account in Linux

There is an easy way to delete the user account on linux which is using userdel command. You can read the manual page of this userdel command by typing ‘man userdel‘ at you terminal as below:

COMMAND NAME
       userdel - delete a user account and related files

SYNOPSIS
       userdel [options] LOGIN

DESCRIPTION
       The userdel command modifies the system account files, deleting all
       entries that refer to login_name. The named user must exist.

OPTIONS
       The options which apply to the userdel command are:

       -f, --force
          This option forces the removal of the user, even if she is still logged
          in. It also forces userdel to remove the userâs home directory or her
          mail spool, even if another user uses the same home directory or if the
          mail spool is not owned by the specified user. If USERGROUPS_ENAB is
          defined to yes in /etc/login.defs and if a group exists with the same
          name as the deleted user, then this group will be removed, even if it
          is still the primary group of another user.

       -r, --remove
          Files in the userâs home directory will be removed along with the home
          directory itself and the user mail spool. Files located in other file
          systems will have to be searched for and deleted manually.

          The mail spool is defined by the MAIL_DIR variable in the login.defs
          file.

Example :

[root@server ~]# userdel -r testroot
[root@server ~]# find / -name testroot
[root@server ~]#

Userdel with option -r will removed user’s home directory and the user’s mail spool

[root@server ~]# userdel demoroot
[root@server ~]# find / -name demoroot
/var/spool/mail/demoroot
/home/demoroot
[root@server ~]#

Userdel without -r will only removed user from /etc/passwd/etc/group and /etc/shadow but the following directory still exist.

/var/spool/mail/demoroot
/home/demoroot

To remove this directory manually, you have to run below command:

[root@server ~]# rm -rf /var/spool/mail/demoroot
[root@server ~]# rm -rf /home/demoroot