Does deleting a user also delete the users home folder Linux?

  • 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

Does deleting a user also delete the users home folder Linux?

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.

If you have ways of finding the userid they used to have (for example because you have one file/directory you know they owned, like their home directory), you can use

find / -uid (userid)

to find all files owned by that user id. You could use find / -uid (userid) -delete to delete them all, but I strongly advise against it without first reviewing what you'd delete. (In all likelihood, it's just their home directory plus some stuff in /tmp.)

If you have no way of finding their userid, you can use

find / -nouser

to find all files belonging to users that don't exist in the system anymore and take an educated guess from the result about file they owned.

What happens when you delete a user in Linux?

You can remove old users from your Linux server. This will revoke the user's SSH access, and remove that user's file and directory ownership.

Which user removes home directory Linux?

Remove a Linux user.
Log in to your server via SSH..
Switch to the root user: sudo su -.
Use the userdel command to remove the old user: userdel user's username..
Optional: You can also delete that user's home directory and mail spool by using the -r flag with the command: userdel -r user's username..

What happens if I delete my Users file?

If you delete a user profile account on your PC, that will delete all the personal files in that account on your PC, the user account folder inside C:\Users folder will be deleted. So, the best option is to backup those files first.

How can a user account be removed without their home directory being affected?

Description. The userdel command removes the user account identified by the login parameter. The command removes a user's attributes without removing the user's home directory by default. The user name must already exist.