How To Delete/Remove User In Ubuntu?

Thursday, Aug 8, 2024 | 3 minutes read | Update at Thursday, Aug 8, 2024

@ İsmail Baydan

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.

  1. Basic user removal using deluser: The deluser command only deletes the user but does not deletes the users home directory. In the following example we delete the user named ismailbut the /home/ismail directory retained.

    sudo deluser ismail
    

    Removes the user ismail but retains their home directory and files.

  2. Basic user removal using userdel: We can use the userdel command in order to remove user account and the users home directory. In the following examle we delete the user ismail and users home directory /home/ismail .

    sudo userdel ismail
    

    Removes the user ismail but retains their home directory and files.

  3. Remove user and their home directory using deluser: By default deluser do not deletes the users home directory but we can delete users directory by adding the --remove-home option to the deluser command.

    sudo deluser --remove-home username3
    

    Removes the user username3 and deletes their home directory.

  4. Remove user and their home directory using userdel: The short form of the --remove-home options for the userdel 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.

  5. Remove user but keep their home directory using deluser: The deluser 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.

  6. 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.

  7. 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 the deluser command.

    sudo deluser --remove-all-files ismail
    

    Removes the user ismail, their home directory, and their mail spool.

  8. Remove user but keep their mail spool using userdel:

    sudo userdel -Z ismail
    

    Removes the user ismail but keeps their mail spool.

  9. Remove a system user using deluser: By default systems users can not be deleted with the deluser command. We can use the --system option in order to remove system user. In the following example we delete the system user named mail.

    sudo deluser --system mail
    

    Removes a system user mail.

  10. 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.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.