userdel – Used to delete a given user and user-related files

The userdel command is a Linux command used to delete a given user and user-related files. When a user is deleted using this command, the user’s home directory and any files owned by the user are also deleted. This command should be used with caution as it permanently deletes the user and all associated files.

Overview

To use the userdel command, simply specify the username of the user to be deleted as an argument. For example, to delete the user “jdoe”, the command would be:

userdel jdoe

By default, userdel will not delete the user’s home directory or any files owned by the user. To delete the user’s home directory and files, use the -r option:

userdel -r jdoe

This will delete the user “jdoe” as well as their home directory and any files owned by the user.

Options

The following table lists all available options for the userdel command:

Option Description
-f, –force This option forces the removal of the user, even if the user is still logged in.
-r, –remove This option removes the user’s home directory and any files owned by the user.
-Z, –selinux-user This option specifies the SELinux user to remove along with the user.

Troubleshooting Tips

If you receive an error message stating that the user cannot be deleted because they are still logged in, use the -f option to force the removal of the user.

If you receive an error message stating that the user’s home directory or files cannot be deleted because they are in use, make sure that the user is not currently logged in and that no processes are running under the user’s account.

Notes

  • The userdel command should be used with caution as it permanently deletes the user and all associated files.
  • Be sure to double-check the username before running the command to avoid accidentally deleting the wrong user.
  • If you only want to delete the user’s home directory and files without deleting the user account, use the rm command instead.