– userdel command in linux is used to delete an existing user and related files. Only the super user can use this command.
– userdel [options] LOGIN
The userdel command modifies the system account files, deleting all entries that refer to the user name LOGIN. The named user must exist.
– First, let’s have a look at the /etc/passwd file to see the list of exiting users
The output shows 5 users. Let us delete the user David
After using the userdel command the user David no more exists in the system.
Deleting the user as shown above only removes the user entry. Therefore, the home directory and the files in the home directory still exist as such in the system.
In order to delete the files within the home directory and the home directory itself use –r option with userdel.
This time user Steve is deleted using –r option. It can be seen that the home directory of Steve is also deleted from the system.
Other options with userdel command in linux
-f : forces the removal of the user even if the user’s logged in. Sometimes as an administrator, you want to delete a user account but are not able to do so because of the user current status. For example:
In such a situation -f option can be used with userdel command to delete the user Steve.
Advice: Do not use this option as it can lead the system in an inconsistent state.
Sample Questions using userdel command in linux
Q1. Can you delete two user U1 and U2 simultaneously using userdel command as below?
#userdel U1 U2
Q2. As an admin you want to delete the account of user Smith and also all the associated files and directories. How can you do it?