80

I just created a new user & its home folder & need to delete it now & also I cant find my old user's Downloads,Documents,Pictures,etc folder in /home/olduser & also .Xauthority file. Dont know how it deleted. I executed startx command by pressing Alt+Ctrl+F3when new user wasnt able to log in.

I deleted user from Users & Groups but its home folder didnt get deleted. How can I fix this?

  • How can I delete new user's home folder safely?

  • How can restore my old documents,downloads,folder?

  • If not then how can I create brand new /home folder & link with OS?

Deepen
  • 4,009
  • 4
  • 22
  • 32
  • What process did you use to add the user? This seems like some very odd behavior. – Chuck R May 02 '14 at 07:13
  • I used `Users & Groups` app downloaded externally from software center (OLD ONE NOT NEW Users Accounts) – Deepen May 02 '14 at 07:14
  • I was not able to locate that package in Software Center. Though, I do have a couple of additional questions for you: 1) Is your home folder encrypted? 2) If so, were you logged in as your main user when you checked the home folder's contents? – Chuck R May 02 '14 at 07:22
  • 1) NO 2) YES, I CHECK WITH THAT USER WHEN I LOGGED AND SAW NONE FOLDERS LIKE DOCUMENTS,DOWNLOADS,ETC – Deepen May 02 '14 at 07:24
  • Very strange indeed. Do you remember the name of a specific file within those folders? You could locate it via `find /home -type f -iname '[full filename]'` – Chuck R May 02 '14 at 07:25

8 Answers8

91

to list all users :

cut -d: -f1 /etc/passwd

To remove user :

sudo userdel username

To remove home directory :

sudo rm -r /home/username

To add a home directory to an existing user :

create a home directory

chown this directory for the user

sudo usermod -d /home/directory user
nux
  • 37,371
  • 34
  • 117
  • 131
  • This is a little more complicated than that. The OP said that something happened to his original account folder during this process. As such, I don't advise that he deletes the new user's home just yet until the data has been located (perhaps, somehow, it got moved into the new user -- we don't know). – Chuck R May 02 '14 at 07:17
  • First time on Askubuntu I found specific answer! Thanx a lot! :) – Deepen May 02 '14 at 07:19
  • I execute sudo usermod -d /home/username username GOT THIS OUTPUT usermod: no changes – Deepen May 02 '14 at 07:21
  • @nux rebooted & worked perfectly fine! thank you! :) – Deepen May 02 '14 at 09:14
61

You can use the more advanced deluser command:

sudo deluser --remove-home user

You can also try the the --remove-all-files option. From man deluser:

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.  Note  that  if you activate both options --remove-home will
have no effect because all files including the home directory and  mail
spool are already covered by the --remove-all-files option.

As can be expected, the second option may take a while to complete.

muru
  • 193,181
  • 53
  • 473
  • 722
  • 15
    Note: This got me into BIG Trouble, as it went into my NFS mount and deleted files created by the same user on other VMs on the NFS file server! Thankfully I stopped it before I lost to much, and extundelete helped a lot as well! (--remove-all-files) – FreeSoftwareServers Oct 17 '16 at 02:47
  • 2
    Well it does say "removes ALL files on the system owned by the user". Great warning though! Unmount any drives or partitions you don't want data deleted from bedore using the `--remove-all-files` option. Or skip option and find the user's files before deleting them to avoid accidents https://www.cyberciti.biz/faq/how-do-i-find-all-the-files-owned-by-a-particular-user-or-group/ – iyrin Jul 31 '22 at 01:56
9

Best way is to use the OPTIONS provided by the userdel command.

sudo userdel -rfRZ <username>

This will:

  1. Force delete

  2. Files in the user's home directory will be removed along with the home directory itself and the user's mail spool. Files located in other file systems will have to be searched for and deleted manually.

  3. Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.

  4. Remove any SELinux user mapping for the user's login.

Hope this helps!

Rush W.
  • 283
  • 1
  • 3
  • 7
1

If you have already deleted the user through the normal userdel <username> and the home directory still exist like i experienced in mine, you just need to run rm -rf /home/<username>

HCM
  • 11
  • 1
  • 3
    `-f` option is really dangerous, if you type the wrong directory, say *Good bye data :(* – damadam Oct 18 '19 at 10:35
  • @damadam true, and that was why I fully stated the argument. To be safe, it is better omitted and prompted during the removal. – HCM Oct 22 '19 at 14:49
1
sudo userdel -r olduser

deletes users homdir and mail spooler

1

Delete/Remove User Account and Files

# deluser --remove-home tecmint      [On Debian and its derivatives]
# userdel --remove tecmint           [On RedHat/CentOS based systems]

But before this there are some other steps:

  1. Lock User Accounts in Linux
# passwd --lock tecmint
  1. Find and Kill All Running Processes of User
# pgrep -u tecmint
# killall -9 -u tecmint
  1. Backup User Data Before Deleting
tar jcvf /user-backups/tecmint-home-directory-backup.tar.bz2 /home/tecmint

source ref: link

Bubba
  • 11
  • 1
0
  1. Go to settings and type users.
  2. Under user click on change settings and type the root password.
  3. You must have another administrator user.
  4. Turn off the administrator option for the user you want to delete.
  5. Click on Remove User at the bottom of the page
Bogo
  • 1
  • 1
0

I LIKE THIS,one-line MASTER COMMAND on my system-administrator job.

sudo Killall -u <username> && sudo deluser --remove-home <username> && sudo 
rm -rm /home/<username>