How To Delete/Remove User In Ubuntu?
Thursday, Aug 8, 2024 | 3 minutes read | Update at Thursday, Aug 8, 2024
Ubuntu provides deluser
and userdel
commands in order to delete/remove users. Even they are very similar they provide different features during user removal. In this tutorial we examine how to delete user in Ubuntu in detail.
-
Basic user removal using
deluser
: Thedeluser
command only deletes the user but does not deletes the users home directory. In the following example we delete the user namedismail
but the/home/ismail
directory retained.sudo deluser ismail
Removes the user
ismail
but retains their home directory and files. -
Basic user removal using
userdel
: We can use theuserdel
command in order to remove user account and the users home directory. In the following examle we delete the userismail
and users home directory/home/ismail
.sudo userdel ismail
Removes the user
ismail
but retains their home directory and files. -
Remove user and their home directory using
deluser
: By defaultdeluser
do not deletes the users home directory but we can delete users directory by adding the--remove-home
option to thedeluser
command.sudo deluser --remove-home username3
Removes the user
username3
and deletes their home directory. -
Remove user and their home directory using
userdel
: The short form of the--remove-home
options for theuserdel
command is the-r
option. We can delete both the user and home directory with the-r
option.sudo userdel -r username4
Removes the user
username4
and deletes their home directory. -
Remove user but keep their home directory using
deluser
: Thedeluser
command removes the user account and home directory completely in a unrecoverable way. But some times we may need to revert back the user acccount and users home directory. The--backup-to
option can be used to backup the users home directory which simply copy the home directory content to the specified path.sudo deluser --backup-to /path/to/backup ismail
Removes the user
ismail
, keeps their home directory, and optionally backs up to a specified location. -
Force removal of user even if they are logged in: During the user removal if the user is already logged in the user removal is failed. If we want to delete user even if he is logged in we should force with the
-f
option.sudo userdel -f ismail
Forces removal of the user
username6
even if they are currently logged in. -
Remove user and their mail spool using
deluser
: The ubuntu users have mail spool in order to store mail data. By default mail spool is not deleted with the deluser command. We can also delete the mail spool of the user with thedeluser
command.sudo deluser --remove-all-files ismail
Removes the user
ismail
, their home directory, and their mail spool. -
Remove user but keep their mail spool using
userdel
:sudo userdel -Z ismail
Removes the user
ismail
but keeps their mail spool. -
Remove a system user using
deluser
: By default systems users can not be deleted with thedeluser
command. We can use the--system
option in order to remove system user. In the following example we delete the system user namedmail
.sudo deluser --system mail
Removes a system user
mail
. -
Remove a user and specify a custom home directory location: Some times the user name and the users home directory name can be different. If we try to delete user with the home directory this will return an error. If the home directory name and user name is different we can specify the home directory name and path with the
--remove-home-dir
option.sudo userdel -r -f --remove-home-dir /custom/home/directory ismail
Removes the user
ismail
and their home directory located at/custom/home/directory
.
Each example above demonstrates a different way to remove a user in Ubuntu, catering to various requirements and scenarios.