How to Delete a User and Home Directory in Ubuntu?

Managing user accounts is a fundamental task for system administrators and Linux enthusiasts alike. When a user no longer requires access to an Ubuntu system, removing their account securely and completely is essential for maintaining system cleanliness and security. This article provides a straightforward, step-by-step guide on how to permanently delete a user account along with their associated home directory using the Ubuntu terminal, ensuring no leftover files occupy valuable disk space.

Prerequisites

Before executing any deletion commands, you must ensure you have the correct privileges. Modifying user accounts requires root or sudo access.

Step 1: Terminate Active User Processes

If the user has background processes running, the deletion command will fail. To safely log the user out and kill all of their active processes, use the killall command.

sudo killall -u username

Note: Replace username with the actual name of the user account you intend to delete throughout this guide.

Step 2: Delete the User and Their Home Directory

Ubuntu provides a dedicated, high-level command line tool called deluser which makes this process incredibly simple. To remove both the user account and their home directory in a single command, use the --remove-home flag.

sudo deluser --remove-home username

When you run this command, Ubuntu will automatically perform the following actions:

Alternative Method: Using userdel

If you prefer using standard low-level Linux utilities that work across almost all distributions, you can use the userdel command with the -r flag. The -r flag instructs the system to remove the home directory and the user’s mail spool.

sudo userdel -r username

Step 3: Verify the Deletion

To confirm that the account and its home directory have been successfully eradicated from the system, you can check the /home directory and the user database.

First, verify that the home directory no longer exists:

ls /home

Second, confirm the user has been removed from the system’s user registry:

grep username /etc/passwd

If the grep command returns no output, the user account has been successfully and permanently deleted from your Ubuntu system.