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: Thedelusercommand only deletes the user but does not deletes the users home directory. In the following example we delete the user namedismailbut the/home/ismaildirectory retained.sudo deluser ismailRemoves the user
ismailbut retains their home directory and files. -
Basic user removal using
userdel: We can use theuserdelcommand in order to remove user account and the users home directory. In the following examle we delete the userismailand users home directory/home/ismail.sudo userdel ismailRemoves the user
ismailbut retains their home directory and files. -
Remove user and their home directory using
deluser: By defaultdeluserdo not deletes the users home directory but we can delete users directory by adding the--remove-homeoption to thedelusercommand.sudo deluser --remove-home username3Removes the user
username3and deletes their home directory. -
Remove user and their home directory using
userdel: The short form of the--remove-homeoptions for theuserdelcommand is the-roption. We can delete both the user and home directory with the-roption.sudo userdel -r username4Removes the user
username4and deletes their home directory. -
Remove user but keep their home directory using
deluser: Thedelusercommand 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-tooption 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 ismailRemoves 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
-foption.sudo userdel -f ismailForces removal of the user
username6even 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 thedelusercommand.sudo deluser --remove-all-files ismailRemoves the user
ismail, their home directory, and their mail spool. -
Remove user but keep their mail spool using
userdel:sudo userdel -Z ismailRemoves the user
ismailbut keeps their mail spool. -
Remove a system user using
deluser: By default systems users can not be deleted with thedelusercommand. We can use the--systemoption in order to remove system user. In the following example we delete the system user namedmail.sudo deluser --system mailRemoves 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-diroption.sudo userdel -r -f --remove-home-dir /custom/home/directory ismailRemoves the user
ismailand 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.