userdel Command: Options, Deleting the Home Directory with -r, and Linux Account Deletion Errors
The userdel command deletes a user account on Linux. Deleting accounts directly on an AlmaLinux 9.8 server and checking the settings on a Debian 12 server as well showed that deleting without options leaves the home directory and mail spool behind, and adding -r deletes them together.
The output below was produced on an AlmaLinux 9.8 server under the root account and on a Debian 12 server under a regular account. Account names and process numbers are example values.
1️⃣ How to Use the userdel Command
The basic form is [userdel options username]. Because it modifies the account information files, run it as root or with sudo.
userdel user1According to the official manual, userdel deletes every entry containing that account name from /etc/passwd, /etc/shadow, and /etc/group. The account name is also removed from the member lists of other groups.
According to the exit code table in the official manual, the exit code is 0 on success, 6 when the account does not exist, and 8 when the account is logged in. Check a deleted account with id; looking up the nonexistent account name user1 on the AlmaLinux server ended as shown below.
id user1id: 'user1': no such userLooking up the same name with getent passwd printed no line and ended with exit code 2. Use this exit code to check in a script whether an account has been deleted.
2️⃣ userdel Options
Print the list of options with userdel --help. shadow-utils 4.9 on the AlmaLinux 9.8 server and userdel on the Debian 12 server printed the same six options.
| Option | Copy | Description |
|---|---|---|
| -r, --remove | Deletes the home directory and mail spool as well | |
| -f, --force | Deletes the account even when it is logged in, along with a home directory and mail spool owned by someone else |
| Option | Copy | Description |
|---|---|---|
| -h, --help | Prints help | |
| -R, --root | Runs chroot into the specified directory and deletes the account inside it | |
| -P, --prefix | Deletes the account from the /etc files under the specified directory, without chroot | |
| -Z, --selinux-user | Deletes the SELinux user mapping of the account |
Run without options, it deletes only the account information and the group named after the account. The commonly used options are -r and -f.
-R and -P are used to modify the account files of a system stored in another directory. -Z applies only to accounts with a separately defined SELinux user mapping.
🔲 userdel Command Simulator
In the simulator below, you can type it yourself: delete an account with userdel, check the remaining home directory and mail spool with ls, and see the deletion error for an account that still has processes.
3️⃣ Practical userdel Command Examples
🔲 Ending Processes and Deleting an Account
The devuser account, created with useradd and given a password validity period with chage, was deleted. A check with pgrep before deletion showed that a sleep process started under that account was still running, and deleting with -r in this state stopped with exit code 8 along with the process number. Looking it up with id showed the account was also still there.
~]# pgrep -u devuser -l
8397 sleep
~]# userdel -r devuser
userdel: user devuser is currently used by process 8397
~]# id devuser
uid=1001(devuser) gid=1001(devuser) groups=1001(devuser),10(wheel)After the account's processes were ended with pkill and the command was run again, the account was deleted along with its home directory and mail spool.
~]# pkill -u devuser
~]# userdel -r devuser
userdel: group devuser not removed because it has other members.
~]# ls -ld /home/devuser
ls: cannot access '/home/devuser': No such file or directory
~]# ls -l /var/spool/mail/devuser
ls: cannot access '/var/spool/mail/devuser': No such file or directory🔲 Common Beginner Mistakes
For the bob account deleted without -r, the home directory and mail spool stayed as they were. The owner field showed UID 1002 instead of the account name, and wheel in the group field is the primary group set with -g when bob was created.
~]# userdel bob
~]# ls -ld /home/bob
drwx------. 2 1002 wheel 4096 Nov 14 07:40 /home/bob
~]# ls -l /var/spool/mail/bob
-rw-rw----. 1 1002 mail 0 Nov 14 07:40 /var/spool/mail/bobDeleting tempuser, which was created without a home directory, with -r printed a line saying the home directory could not be found, but the exit code was 0 and the account was deleted.
~]# userdel -r tempuser
userdel: tempuser home directory (/home/tempuser) not foundEntering an account name that had already been deleted ended with exit code 6 and a message saying the account does not exist.
~]# userdel devuser
userdel: user 'devuser' does not exist🔲 Reading the Results of userdel
userdel prints nothing on success, so when a line appears, check what it reports together with the exit code. Because alice had been added to the devuser group with usermod -aG before deletion, deleting devuser printed one line, [userdel: group devuser not removed because it has other members.], and only the group remained. The remaining line was checked with getent group and then deleted with groupdel.
~]# getent group devuser
devuser:x:1001:alice
~]# groupdel devuserIn the member list of the wheel group added with -G, devuser was removed and only the accounts that were there before remained.
~]# grep -E '^wheel:' /etc/group
wheel:x:10:linuxuserYou can try userdel, the -r option, and checking the remaining home directory yourself in the userdel Command Simulator.
4️⃣ Deleting the Home Directory and Mail Spool with userdel -r
The -r option deletes the home directory, the files in it, and the mail spool along with the account.
userdel -r user1The location of the mail spool is set by the MAIL_DIR value in /etc/login.defs.
grep -E '^MAIL_DIR' /etc/login.defsMAIL_DIR /var/spool/mailMAIL_DIR on the AlmaLinux 9.8 server was /var/spool/mail, and on the Debian 12 server it was /var/mail. On the Debian 12 server, /var/spool/mail is a link to /var/mail, so both paths are the same directory.
The useradd default on the Debian 12 server was CREATE_MAIL_SPOOL=no, which creates no mail spool file. Deleting an account without a mail spool file with -r prints [userdel: user1 mail spool (/var/mail/user1) not found].
The home directory and mail spool of an account deleted without -r stay as they are. The owner of the remaining files appears as a UID number instead of the account name in [ls -l].
The official manual states that even -r does not delete files on other file systems, so they must be found and deleted manually. After deleting an account, find files with no owner using -nouser of find.
find / -xdev -nouser-xdev is an option that keeps find from descending into other file systems, so on a server with several disks attached, run it for each mount point.
5️⃣ Linux Account Deletion Errors and Forced Deletion with -f
If a process of the account is running when the account is deleted, userdel does not delete the account and ends with the message below.
userdel: user user1 is currently used by process 23452345 is an example process number, and the exit code of userdel in this case is 8.
Check the account's processes with pgrep. -u is the option that searches by the running account, and -l is the option that also prints the process name.
pgrep -u user1 -lIf the account does not exist, the same command ends with an error. Looking up the nonexistent account name no-such-user1 on the AlmaLinux server printed [pgrep: invalid user name: no-such-user1].
The official manual advises ending the processes first in this case, or locking the password or account and deleting it later. To end all of the account's processes at once, use [pkill -u].
pkill -u user1-f is the option that goes ahead with deletion even when the account is logged in. The official manual's description of -f says nothing about ending processes, and it carries a warning that this dangerous option can leave the system in an inconsistent state.
userdel -f user1Used with -r, -f deletes the home directory even when another account uses the same home directory or the mail spool has a different owner. The processes of an account deleted with -f cannot be found by name, so search with the UID instead of the account name in [pgrep -u].
On the AlmaLinux 9.8 server, a sleep process was started under the newly created tempuser account, which had a home directory, and the account was then deleted with -f. The same in-use message appeared as without -f, but the exit code was 0, and looking it up with id showed the account had been deleted.
~]# userdel -f tempuser
userdel: user tempuser is currently used by process 21569
~]# id tempuser
id: ‘tempuser’: no such user
~]# pgrep -u 1001 -l
21569 sleep
~]# ls -ld /home/tempuser
drwx------. 2 1001 1001 4096 Nov 14 08:13 /home/tempuserThe remaining sleep process could be found only by UID 1001, and since the account was deleted without -r, its home directory also remained with the owner field changed to the UID. When the bob account was then created anew, the same UID 1001 was assigned again, so the leftover sleep process appeared as one of bob's processes in [pgrep -u bob]. So after deleting with -f, clean up the processes with the same UID first.
6️⃣ Group Deletion with userdel and USERGROUPS_ENAB
If USERGROUPS_ENAB in /etc/login.defs is yes, userdel also deletes the group with the same name as the account. It was yes on both the AlmaLinux 9.8 server and the Debian 12 server.
grep -E '^USERGROUPS_ENAB' /etc/login.defsUSERGROUPS_ENAB yesIf other accounts are members of that group, the group remains and [userdel: group user2 not removed because it has other members.] appears. When the group is used as another account's primary group, [userdel: group user2 is the primary group of another user and is not removed.] appears and the group remains as well.
Adding -f deletes the group even when it is another account's primary group.
Check remaining groups with getent group.
getent group user2After checking the member list at the end of the line as well, delete the group separately with groupdel.
groupdel user2According to the official manual, groupdel does not delete the primary group of an existing account. If an account uses that group as its primary group, delete the account first and then the group.
7️⃣ Differences Between userdel and deluser
deluser is a command in the adduser package of the Debian family. The official manual introduces deluser as a program in front of userdel and groupdel that adds features such as deleting the home directory.
dpkg -S /usr/sbin/userdel /usr/sbin/deluserpasswd: /usr/sbin/userdel
adduser: /usr/sbin/deluserOn the Debian 12 server, userdel was in the passwd package and deluser was in the adduser package. The AlmaLinux 9.8 server had no deluser.
ls -l /usr/sbin/deluserls: cannot access '/usr/sbin/deluser': No such file or directoryThe defaults of deluser are set in /etc/deluser.conf. The three lines that set the deletion scope were looked up on the Debian 12 server.
grep -E '^#?(REMOVE_HOME|REMOVE_ALL_FILES|BACKUP) ' /etc/deluser.conf#REMOVE_HOME = 0
#REMOVE_ALL_FILES = 0
#BACKUP = 0All three lines were 0 and commented out. So deluser also deletes only the account when run without options, and deletes the home directory and mail spool only when --remove-home is added.
deluser --remove-home user1--remove-all-files deletes all files owned by the account, including files outside the home directory. According to the official manual, deluser refuses to delete the root account.
🔢 FAQ & Recommended Content
Checking logins
Linux who, w Commands & user Command
A post that summarizes how to check with who and w which accounts are logged in to the server now and what tasks those accounts are running.
Ending processes
Linux killall Command: Installation, Usage and Options
A post that summarizes how to end processes with the same name all at once with killall, how to install it, and its main options.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.