How to use the deluser command on Ubuntu Linux with examples

The deluser command is used by system administrators to delete or remove user accounts from Linux systems, including Ubuntu.

Whether you’re a new user or student learning how to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS.

Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.

When you’re ready to learn how to use the deluser commands, follow the guide below:

About deluser command:

The deluser command is used by system administrators to delete or remove user accounts from Linux systems, including Ubuntu. a friendly and alternative command is userdel.

Like using your mouse and keyboard to delete accounts in the GUI. the deluser is the way to do it on the command line.

By default, the deluser command deletes or removes users without deleting or removing their home directories and mail spools.

Syntax:

The syntax is the rule and format of how the deluser command can be used. the syntax’s options can be reordered. but straight format must be followed.,.

Below is an example syntax of how to use the deluser command.

userdel [options] LOGIN.

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the command. they are separated by spaces and followed after the command’s options.

Below are some options of the deluser command:

 LOGIN.Replace LOGIN .. with the name of the account you want to delete. If the account doesn’t already exist, the command will fail and not execute..
-f, –force
Use the -f or –force option to force the removal of the user’s files.
-r, –removeUse the -r or –remove option to remove home directory and mail spool.
-R, –root CHROOT_DIRUse the -R or –root CHROOT_DIR option to specify thedirectory to chroot into
 -Z, –selinux-userUse the -g or –gid GROUP option to remove any SELinux user mapping for the user

Examples:

Below are some examples of how to run and use the deluser on Ubuntu Linux.

If you want to delete an account name John, run the commands below.

deluser john

If you wish to delete or remove John’s account including deleting his home directory, run the commands below.

deluser --remove-home john

If you’re not logged in as a root account, you may have to use the sudo command it it.

sudo deluser --remove-home john

To you want to delete John’s account even while he’s logged in, run the commands below.

sudo deluser --force john

To delete John’s account and backup his home directory, run the commands below.

sudo deluser --backup-to /backup_directory john

The commands above will delete the user account and create an archive of the user’s home directory into the path you specified.

When you run deluser with the –help option, you’ll see the help text below:

Usage: userdel [options] LOGIN

Options:
  -f, --force                   force removal of files,
                                even if not owned by user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
  -Z, --selinux-user            remove any SELinux user mapping for the user

That’s it!

Hope you like it and please come back soon for more Ubuntu Linux command!

You may also like the commands below: