0
0
Linux-cliHow-ToBeginner · 3 min read

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:

  • sudo runs the command with administrator rights.
  • userdel is the command to delete a user.
  • username is the name of the user to delete.
  • -r option 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 -r if 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 files
📊

Quick Reference

CommandDescription
sudo userdel usernameDelete user without removing files
sudo userdel -r usernameDelete user and remove home directory and mail
whoCheck logged in users
pkill -u usernameKill 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.