Skip to content

userdel Linux command

The Linux userdel command is used to remove user accounts from the system. When you run the userdel command, it deletes the user’s entry from the system files and removes the user’s home directory. It is an essential command for system administrators to manage user accounts on a Linux system. By using userdel, you can ensure that old or unused accounts are removed securely from the system, improving security and managing resources more effectively. However, it is crucial to use the userdel command with caution to avoid unintended consequences.

userdel Syntax:

Terminal window
userdel [options] username

Options:

OptionDescription
-fForce deletion of the user account
-rRemove the user’s home directory
-ZRemove any SELinux user mapping

Parameters:

ParameterDescription
usernameThe username to be deleted

userdel bash Examples:

Delete a User

Terminal window
userdel john

Removes the user account named “john” from the system.

Delete a User and Home Directory

Terminal window
userdel -r jane

Deletes the user account “jane” along with the associated home directory and mail spool.

Remove a User without Deleting Home Directory

Terminal window
userdel -r --keep-primary-group alex

Deletes the user “alex” but retains the primary group and home directory.

Change User Login to Nologin

Terminal window
userdel -s /sbin/nologin sarah

Changes the login shell of user “sarah” to /sbin/nologin.

Remove User with Specified UID

Terminal window
userdel -f -o -uid 1001 mike

Forces the removal of a non-unique UID user “mike” with UID 1001.

Delete User Without Prompt

Terminal window
userdel -f tom

Deletes the user account “tom” without prompting for confirmation.

userdel Command Help Center:

How do I use userdel in Linux?

To use the userdel command in Linux, execute the following command:

Terminal window
userdel username

Can userdel remove a user’s home directory?

Yes, the userdel command can delete a user’s home directory along with the user account using the following command:

Terminal window
userdel -r username

How can I force the deletion of a user’s account using userdel?

To force the deletion of a user’s account with userdel, use the following command:

Terminal window
userdel -f username

How can I list the options available for the userdel command in Linux?

To list the options available for the userdel command, you can check the userdel manual by executing:

Terminal window
man userdel

Can userdel delete multiple user accounts at once?

Yes, userdel can delete multiple user accounts at once by providing a list of usernames separated by spaces like in the following command:

Terminal window
userdel username1 username2

Is it possible to prevent userdel from deleting the user’s mail spool?

Yes, by using the -m option with userdel, you can prevent the user’s mail spool from being deleted. Here is an example:

Terminal window
userdel -m username

How do I remove a user account without deleting the home directory?

To remove a user account without deleting the home directory, you can use the -r option with userdel as shown below:

Terminal window
userdel -r username

How can I display a message before deleting a user account with userdel?

You can display a message before deleting a user account by using the -c option with userdel. Here’s how you can do it:

Terminal window
userdel -c "Your account will be deleted" username

Applications of the userdel command

  • Delete a user account from the system
  • Remove the user’s home directory and mail spool
  • Clean up the user’s entry from system files like /etc/passwd, /etc/shadow, and /etc/group