3

I'm on 12.04 desktop and was wondering if the following command would remove all the data from my user home directory as well as my trash can:

rm -r /home/myUser/*

If so, can someone confirm that the -r flag will prevent Ubuntu from asking me about each and every file? And if not, what are the correct command(s) for doing so? I understand that this won't clear out certain parts of my hard drive - I'm not looking for that. Just looking to make sure no more files exist on the file system under that directory and its respective trash can. Thanks in advance!

0

3 Answers 3

2

That will work perfectly, but to prevent any problems, you should add -f too, as in this example:

# rm -rf /home/exampleuser/*
1
  • This unfortunately won't delete hidden files and folders in your home dir, including the trash bin in ~/.local/share/Trash, since the glob character, *, won't automatically match hidden files. I added an answer which covers this and I've fully tested it on a real system. Mar 6, 2023 at 17:45
0

Yes!

use:

rm -rf /home/myUser/*

f stands for force so it will never ask you anything and r stands for recursive i.e. it will recursively delete every directory see

man rm

With this command, you can clear all the data from user home directory as well as your trash (because trash is located at /home/user/.local/share/Trash)

But remember! This step cannot be undone it will delete all your data

0

This answer has been fully tested on a real system, three times: on Ubuntu 18.04, 20.04, and 22.04.

How to fully delete (or reset) your home dir at ~ (or /home/my_username), even while actively logged into that user

There's no teacher better than experience.

WARNING WARNING WARNING: do not run any of the rm commands below unless you want to wipe your whole user home directory or system! This is to do a full user wipe and reset, if that is what you want. Also, UNPLUG ANY EXTERNAL DRIVES, AND UNMOUNT ANY MOUNTED DISKS IN YOUR HOME DIR, BEFORE RUNNING THE rm COMMAND OR ELSE THIR CONTENTS WILL BE DELETED TOO!

Nope! Doing the command you said will not work as desired. I literally just tried this for a user I wanted to reset. That's right. I actually ran it. rm -rf /home/my_username/* does not work, because the shell glob pattern * does not match hidden files and folders! That means it leaves in a ton of your data, including your trash can, which is at ~/.local/share/Trash. Hidden files and folders begin with a dot (.), and using the glob pattern /home/my_username/* leaves in all hidden files and folders immediately in your home dir.

Therefore, do this instead:

The quick answer:

# First, CLOSE ALL OPEN PROGRAMS, such as Chrome, editors, etc.
# Leave open only one terminal to run the following commands in.

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING
# - Do NOT accidentally run this on the wrong computer while testing!
# - see the big warning above. 
# - UNPLUG ANY DRIVES FIRST! 
# - And UNMOUNT ANY MOUNTED DISKS IN YOUR HOME DIR FIRST!
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# Delete your entire home folder (see warning at start of this answer).
sudo rm -rf /home/my_username   # WARNING WARNING WARNING

# WARNING WARNING WARNING WARNING WARNING WARNING WARNING

Now, do some "cleanup" and recreate the home dir to "reset" it for the user:

# Now, the following assumes you want to recreate an empty home dir now, and you
# are already logged in under the username whose folder you just deleted
# above.

# recreate an empty home dir
sudo mkdir /home/my_username
# ensure your group name exists (wiping your home dir may have removed it I
# think, as that seems to be what happened to me just now)
sudo groupadd my_username
# add your username back to this group
sudo usermod -a -G my_username my_username

# give your username ownership over the new, empty home dir
sudo chown -R my_username:my_username /home/my_username
# ensure everything looks right (you should see the user and group ownership
# of your new home dir is 'my_username my_username')
ls -alF /home

# (Optional, but recommended) restore Ubuntu's default `~/.bashrc` and
# `~/.profile` type bash terminal configuration files
cp /etc/skel/.bash_logout ~
cp /etc/skel/.bashrc ~
cp /etc/skel/.profile ~
# Now re-source these files to refresh your terminal
. ~/.profile

# now open your file manager, such as nemo 
# (see: https://askubuntu.com/a/1173861/327339)
# and check inside your home dir; you'll see Ubuntu automatically has already
# recreated a ~/Desktop dir for you.
ls -alF ~

Now you're done. If you left programs like Chrome open while you did this, Chrome would have kept running and may have already restored some of your user's files, so...close Chrome and start over if that's not what you want. From here on out, just opening programs will start recreating some of the deleted files and dirs for you again. Ex: open Chrome and you'll see no user is logged in anymore, but just opening the program recreates these dirs:

/home/my_username/.cache
/home/my_username/.config
/home/my_username/.local

Now log out, or restart your computer, and log back in and you'll see it's almost as though you had just created a new user. Your "start menu" (launcher) will still have all of the programs you previously installed system-wide, but user-only programs and settings and files will all be wiped. You'll even notice the following:

  • your username still exists, you can still log in, and it still has the same password; but:
  • your mouse speed settings have been reset
  • your external monitor position and settings have been reset
  • your custom Ubuntu shortcut keys are gone
  • Chrome and other apps no longer have any stored users or settings

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .