userdel Command: Options, Deleting the Home Directory with -r, and Linux Account Deletion Errors

Test environmentRHEL familyAlmaLinux 9.8shadow-utils 4.9root accountDebian 12 regular account
Users · groupsuserdelLocation/usr/sbin/userdel

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 user1

According 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 user1
id: 'user1': no such user

Looking 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.

OptionCopyDescription
-r, --removeDeletes the home directory and mail spool as well
-f, --forceDeletes the account even when it is logged in, along with a home directory and mail spool owned by someone else
OptionCopyDescription
-h, --helpPrints help
-R, --rootRuns chroot into the specified directory and deletes the account inside it
-P, --prefixDeletes the account from the /etc files under the specified directory, without chroot
-Z, --selinux-userDeletes 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.

userdel Command Simulatorshadow-utils 4.9
[root@server work]#
This is a virtual AlmaLinux server that runs only inside the browser; remaining accounts appear in the table on the left and home directories in the /home tree, and ↑↓ recalls the commands you typed earlier.

3️⃣ Practical userdel Command Examples

  • ↪️ Deleting an account with remaining processes and common beginner mistakes

🔲 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/bob

Deleting 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 found

Entering 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 devuser

In 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:linuxuser

You 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 user1

The location of the mail spool is set by the MAIL_DIR value in /etc/login.defs.

grep -E '^MAIL_DIR' /etc/login.defs
MAIL_DIR	/var/spool/mail

MAIL_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 2345

2345 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 -l

If 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 user1

Used 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/tempuser

The 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.defs
USERGROUPS_ENAB yes

If 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 user2

After checking the member list at the end of the line as well, delete the group separately with groupdel.

groupdel user2

According 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/deluser
passwd: /usr/sbin/userdel
adduser: /usr/sbin/deluser

On 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/deluser
ls: cannot access '/usr/sbin/deluser': No such file or directory

The 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 = 0

All 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

userdel --help has no backup option, so before deleting with -r, compress the home directory first, as in [tar -czf /root/user1-home.tar.gz -C /home user1]. According to the official manual, deluser in the Debian family creates a tar archive named after the account before deletion when --backup is added.

The official manual explains how to set, with USERDEL_CMD, a script that removes the account's cron and at jobs when the account is deleted, and this line was commented out in /etc/login.defs on both servers. On a server where it is not set, first delete the account's cron jobs with [crontab -r -u username] before deletion.

To only block logins without deleting the account, lock the password or the account. [passwd -l username] locks only the password, so the account can still log in with other authentication such as SSH keys; to block the whole account, use [chage -E 0 username].

ℹ️ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 6   +   6   =