how to delete a user on Linux

How to on Linux: Delete a User

A new tutorial in our series of simpler tutorials for Linux beginners: how to delete a user. In this tutorial, we’re going to show you how to remove users via the command-line interface (CLI, Terminal).

For this tutorial, you’ll have to be logged in as the root user or a user with sudo privileges.

If you’re using a GUI, either open up a Terminal and use the commands below or just find the settings menu where you can delete users. For Ubuntu, it would be something like Activities -> Users -> Add or remove users -> Unlock -> Click on the user and click “Remove User”.

How to delete a user on Linux using the userdel command

For this tutorial, we’ll be using the userdel command.

The basic syntax is:

userdel [-options] [username]

The most basic example would be, if you wanted to delete a user with the username “stan21”, you’d run:

userdel stan21

We’ll explore more options and use cases below.

How to delete a user on Linux including their home directory and mail spool

If you wanted to delete a user and delete their home directory and mail spool along the way, you can do it with a single command, by passing the -r option. For example, this command:

userdel -r stan21

Will delete the user “stan21” along with their home directory and mail spool. This doesn’t delete other files or directories.

How to delete a user on Linux if they are currently logged in or have running processes

If you tried to run the userdel command when a user has running processes, you’d get a message like “userdel: user stan21 is currently used by process 72612”.

If a user is currently logged in or if they have running processes, you can delete the user and log them out/kill their processes by using the -f option.

Example:

userdel -f stan21

This will delete all running processes by the user “stan21”, log them out, and delete the user.

How to combine multiple options/flags with the userdel command

You can also combine both options, like:

userdel -r -f stan21

This command combines the commands explained in the previous 2 examples.

A bonus option/flag is the -Z option, which removes any SELinux user mapping for the user.

If you run:

userdel -h

You’ll get a help menu with all the options and their explanations.

And that’s all. It’s a pretty simple command and pretty simple usage. It gets somewhat complicated if you want to completely remove all traces from the user – all files, all directories, everything. We’ll write a detailed tutorial on that sometime in the future.

Leave a comment

Your email address will not be published.

One thought on “How to on Linux: Delete a User”