Bash Script to Delete User Account Safely
Use the Bash command
sudo userdel -r username to delete a user account and its home directory; in a script, replace username with the target user.Examples
Inputusername=john
OutputUser 'john' deleted along with home directory.
Inputusername=alice
OutputUser 'alice' deleted along with home directory.
Inputusername=nonexistent
Outputuserdel: user 'nonexistent' does not exist
How to Think About It
To delete a user account in Bash, the script needs to accept the username, check if the user exists, then run the system command
userdel with the -r option to remove the user and their home directory safely.Algorithm
1
Get the username input from the user or argument2
Check if the user exists on the system3
If user exists, run <code>sudo userdel -r username</code> to delete user and home directory4
Print success or error message based on the command resultCode
bash
#!/bin/bash if [ "$#" -ne 1 ]; then echo "Usage: $0 username" exit 1 fi username=$1 if id "$username" &>/dev/null; then sudo userdel -r "$username" && echo "User '$username' deleted along with home directory." || echo "Failed to delete user '$username'." else echo "User '$username' does not exist." fi
Output
User 'john' deleted along with home directory.
Dry Run
Let's trace deleting user 'john' through the code
1
Check argument count
Input arguments count is 1, proceed.
2
Check if user exists
Command id john succeeds, user exists.
3
Delete user
Run sudo userdel -r john, command succeeds.
| Step | Action | Result |
|---|---|---|
| 1 | Check argument count | 1 argument found, OK |
| 2 | Check if user exists | User 'john' exists |
| 3 | Delete user | User 'john' deleted successfully |
Why This Works
Step 1: Check input argument
The script uses $# to ensure exactly one username is provided to avoid errors.
Step 2: Verify user existence
The id command checks if the user exists before attempting deletion to prevent errors.
Step 3: Delete user safely
The userdel -r command removes the user and their home directory, cleaning up all user data.
Alternative Approaches
Delete user without removing home directory
bash
#!/bin/bash username=$1 if id "$username" &>/dev/null; then sudo userdel "$username" && echo "User '$username' deleted without home directory." || echo "Failed to delete user '$username'." else echo "User '$username' does not exist." fi
This keeps the user's home directory and files intact, useful if you want to preserve data.
Force delete user even if logged in
bash
#!/bin/bash username=$1 if id "$username" &>/dev/null; then sudo userdel -r -f "$username" && echo "User '$username' force deleted." || echo "Failed to force delete user '$username'." else echo "User '$username' does not exist." fi
The <code>-f</code> option forces deletion even if the user is logged in, but use with caution.
Complexity: O(1) time, O(1) space
Time Complexity
The script runs a fixed number of system commands regardless of input size, so time complexity is constant O(1).
Space Complexity
The script uses a few variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches run system commands with similar speed; forcing deletion may be slightly slower due to extra checks.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Delete with home directory (-r) | O(1) | O(1) | Complete user removal |
| Delete without home directory | O(1) | O(1) | Preserving user files |
| Force delete (-f) | O(1) | O(1) | Removing logged-in users |
Always check if the user exists before deleting to avoid errors.
Trying to delete a user without sudo privileges causes permission errors.