How to Delete User in Linux: Simple Command Guide
To delete a user in Linux, use the
userdel command followed by the username. For example, sudo userdel username removes the user, and adding -r deletes the user's home directory and files.Syntax
The basic syntax to delete a user in Linux is:
sudo userdel [options] username
Where:
sudoruns the command with administrator rights.userdelis the command to delete a user.usernameis the name of the user to delete.-roption removes the user's home directory and mail spool.
bash
sudo userdel username sudo userdel -r username
Example
This example shows how to delete a user named john and remove his home directory and files.
bash
sudo userdel -r john
Common Pitfalls
Common mistakes when deleting users include:
- Trying to delete a user who is currently logged in, which may fail or cause issues.
- Not using
-rif you want to remove the user's files, leaving orphaned files. - Running the command without
sudo, causing permission denied errors.
Always check if the user is logged in with who or w before deleting.
bash
sudo userdel john # May fail if user is logged in
# Correct approach:
sudo pkill -u john # Kill all processes of john
sudo userdel -r john # Then delete user and filesQuick Reference
| Command | Description |
|---|---|
| sudo userdel username | Delete user without removing files |
| sudo userdel -r username | Delete user and remove home directory and mail |
| who | Check logged in users |
| pkill -u username | Kill all processes of the user |
Key Takeaways
Use
sudo userdel -r username to delete a user and their files safely.Ensure the user is not logged in before deleting to avoid errors.
Always run
userdel with sudo for proper permissions.Use
pkill -u username to stop user processes before deletion if needed.Check logged-in users with
who or w commands.