Which command is used to delete users account and home directory?

If you want to remove a user account from the system, use the userdel command on the command line as root.

userdel username

Combining userdel with the -r option removes files in the user's home directory along with the home directory itself and the user's mail spool. Files located in other file systems have to be searched for and deleted manually.

userdel -r username

Note

The -r option is relatively safer, and thus recommended, compared to -f which forces the removal of the user account even if the user is still logged in.

  • Delete User Account with Home Directory
  • Lock User Account
  • Find and Kill all Running Process of a Particular User
  • Backup User Data
  • Delete User Account and their Data

As Linux Administrator you may have to remove users account when a user leave organization or a company for any reason. If you remove user’s account it is also important to remove their home directory to free up some space on the storage device.

In this article I will show you how to delete user’s account together with his/her home directory in Linux/Unix system.

To complete this tutorial I am going to create two user account the my Linux system called “sagar” and “linuxuser” with their home directory /home/sagar and /home/linuxuser using adduser command.

# adduser sagar
# passwd sagar
# adduser linuxuser
# passwd linuxuser

Above you can see I have created two user with adduser command you can also use useradd command to create user account both are same and does the same job.

Before delete users with their home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that user can only be deleted effectively when they are not logged on to the server.

Lock User Account

First of all lock the user account password that you want to delete, so that there is no access for the user to the system. This will also prevent a user from running process on the system.

# passwd --lock sagar
Locking password for user sagar.
passwd: Success

Find and Kill all Running Process of a Particular User

Now find the all running process of a particular user and kill them by determine the PIDs of process owned by the user using below command:

# pgrep -u sagar
1847
1859
1991
1994
1995

You can also list full details using below command such as username, PIDs, PPIDs, Terminal used, Process state, command path etc.

# ps -f --pid $(pgrep -u sagar)
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
sagar   1847     1  0 10:49 ?        SLl    0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
sagar   1859  1280  0 10:49 ?        Ssl    0:00 mate-session
sagar   1991  1959  0 10:49 ?        Ss     0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session
sagar   1994     1  0 10:49 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session
sagar   1995     1  0 10:49 ?        Ss     0:00 //bin/dbus-daemon --fork --print-pid 6 --print-address 9 --session

Once finding the all running process of a particular user now you can use killall command to kill those process like below:

# killall -9 -u sagar

Where -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines username.

Backup User Data

You can keep user data backup before deleting it. It is important for future purpose if a new user join a company you can copy all user directory and files to new user’s home directory.

Use tar utilities to create a backup of users home directory like below:

# tar jcvf /opt/user-bkp/sagar-home-directory-backup.tar.bz2 /home/sagar

Delete User Account and their Data

Now you can remove user account and his/her home directory using –remove-home option on Debian system, you can also try –remove-all-files. For CentOS/RedHat user –remove in the command line.

# deluser --remove-home sagar [ For Debian System]
# userdel --remove sagar      [ For CentOS/RedHat System]

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

You may also like

About the author

Which command is used to delete users account and home directory?

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

What command is used to delete user and his home directory?

userdel -r: Whenever we are deleting a user using this option then the files in the user's home directory will be removed along with the home directory itself and the user's mail spool. All the files located in other file systems will have to be searched for and deleted manually.

Can we delete a user and still have his home directory?

By default, deluser will remove the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the --remove-home option. The --remove-all-files option removes all files on the system owned by the user.

How do I remove a user from a directory?

Delete User Profile in Windows 10 Manually in 6 Steps.
Open the File Explorer window. ... .
Open the Users folder. ... .
Delete the Required User Profile. ... .
Open Registry Editon. ... .
Navigate to ProfileList. ... .
Remove ProfileImagePath linked to the Deleted User Profile..

How do I delete a directory at home?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.