userdel


userdel (delete a user account)

The userdel command is used to delete a user account from the system. It can also remove the user's home directory and mail spool, ensuring a clean removal of the user's data. This command must be used with caution, as deleting a user account is irreversible.


Example 1: Delete a user account

# Delete the user account 'jdoe' from the system
sudo userdel jdoe

Explanation

This command removes the user jdoe's entry from the /etc/passwd and /etc/shadow files. However, it does not remove the user's home directory or other files they may own.


Example 2: Delete a user and their home directory

# Delete the user 'msmith' and remove their home directory and mail spool
sudo userdel -r msmith

Explanation

The -r option is crucial for a complete removal. It tells userdel to delete the user's home directory (/home/msmith) and their mail spool file, in addition to deleting the account itself.


Example 3: Forcibly delete a user even if logged in

# Force the deletion of user 'asmith' even if they are currently logged in
sudo userdel -f asmith

Explanation

The -f option forces the deletion of the user account. This can be necessary if the user has running processes that would otherwise prevent the account from being removed. This should be used with extreme care.


Example 4: Delete a user and remove their group if it's empty

# Delete user 'rjay' and also remove the 'rjay' group if no other members exist
sudo userdel -r --remove-subgid rjay

Explanation

The --remove-subgid option is not standard on all systems, but in RHEL, you can combine userdel with groupdel. A more common approach is to manually remove the group after deleting the user. A safer combined command is userdel -r rjay && groupdel rjay.


Example 5: A safer way to remove a user and their group

# First, delete the user 'llee' and their home directory
sudo userdel -r llee

# Then, delete the user's primary group if it exists and is empty
sudo groupdel llee

Explanation

Separating the user and group deletion is a more deliberate and safer approach. This two-step process ensures you don't accidentally remove a group that other users might still belong to.


Example 6: Previewing files to be deleted

# Find all files owned by the user 'guest' before deleting them
find / -user guest

# After reviewing, delete the user and their home directory
sudo userdel -r guest

Explanation

Before using userdel -r, it's a good practice to use the find command to locate all files owned by the user across the entire filesystem. This helps you understand the impact of the deletion and avoid unintended data loss.


Example 7: Deleting a user who owns files outside their home directory

# Find and change ownership of files owned by 'testuser' outside their home
sudo find / -user testuser -exec chown root:root {} \;

# Now, safely delete the user 'testuser'
sudo userdel -r testuser

Explanation

If a user owns important system files, deleting them can cause problems. This example first finds all files owned by testuser and changes their ownership to root. After securing the files, the user account is deleted.


Example 8: Using deluser as an alternative

# Use the deluser command, which is a friendlier frontend to userdel
sudo deluser --remove-home jdoe

Explanation

Debian-based systems often use deluser, which is also available on RHEL. It provides a more interactive and sometimes safer way to remove users, often backing up files before deletion by default.

UNIX Variants

Solaris: The command is the same: userdel.

FreeBSD: The command is rmuser (interactive) or pw userdel.