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.

Terminal window
userdel [options] username
OptionDescription
-fForce deletion of the user account
-rRemove the user’s home directory
-ZRemove any SELinux user mapping
ParameterDescription
usernameThe username to be deleted
Terminal window
userdel john

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

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

Section titled “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.

Terminal window
userdel -s /sbin/nologin sarah

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

Terminal window
userdel -f -o -uid 1001 mike

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

Terminal window
userdel -f tom

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

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

Terminal window
userdel username

Can userdel remove a user’s home directory?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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
  • 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