How to use the groupdel Command

In this guide, I will go through everything you need to know to use the groupdel command on a Linux operating system.

How to use the groupdel Command

The groupdel command enables you to remove specific groups from a Linux operating system. After invoking the command, it will attempt to remove all entries that refer to the specified group from all the system account files. If you maintain a system with many users and groups, this command will eventually come in handy.

There are a few downsides to using this command. For example, the group must not be a primary group for any user. Otherwise, you will need to remove the user first. You will also need to search and manually check for any files owned by the group, as the command will not update their file permissions.

The guide below will cover the commands syntax, options, exit codes, and how you can use the command. All these topics cover almost everything you need to know about the groupdel command.

groupdel Command Syntax

The syntax of the groupdel command is very straightforward. The command will accept options and the name of the group you wish to delete.

groupdel [OPTIONS] group
  • [OPTIONS] is where you can specify any options that you would like applied to the command.
  • group is the name of the group you wish to delete.

groupdel Command Options

The groupdel command features only a few options. Below are the options that you can use with the command.

  • -f or --force will force the deletion of the group, even if it’s the primary group for a user.
  • --extrausers will make groupdel use the extra user’s database.
  • -h or --help will display brief help information before exiting.
  • -R or --root applies the changes in the CHROOT_DIR directory and uses configuration files from within the same directory.
  • -P or --prefix applies the changes in the PREFIX_DIR directory and uses configuration files from within the same directory. Does not support SELinux (Security-Enhanced Linux).

Command Exit Codes

There is a range of exit codes that the command may return. These codes typically indicate whether the task was successful or it failed.

You can check the exit value by entering echo $? after the command has run.

  • 0 Success
  • 2 Invalid command syntax
  • 6 specified group does not exist.
  • 8 Can’t remove the user’s primary group.
  • 10 Can’t update the group file.

Using the groupdel Command

Using the groupdel command is easy, so we will take you through a few basic examples to demonstrate how to delete a group using the command.

To delete a group, specify the groupdel command followed by the group name you wish to delete. You will need to ensure that the group is not a primary group of a user. Otherwise, the deletion will fail. The -f or --force option will allow you to delete a primary group, but it is not recommended.

For the command to work, you must be the root user or a user with sudo privileges. Otherwise, you will see an error similar to “groupdel: cannot lock /etc/group; try again later“.

The command below will delete the group named exampleGroup from our Linux system.

sudo groupdel exampleGroup

The above command will not provide any feedback unless it runs into an error, such as the group not existing.

You can confirm the group is deleted by using the following less command.

less /etc/group

If the command worked correctly, you would not see the group in the list printed to the terminal.

Also, attempting to delete the group again will give you an error like the example below.

dev@pimylifeup:~$ sudo groupdel exampleGroup
groupdel: group 'exampleGroup' does not exist

More Help

If you require more help on how to use this command and its options, there are two different methods that you can use to get more information.

The first method uses the man command to bring up the manual information for the groupdel command. To do this, enter the following command.

man groupdel

You can exit the man pages by pressing Q.

Using the --help option will print some basic help information to the terminal. You can do this by entering the following command into the terminal.

groupdel --help

Conclusion

I hope you now have a decent understanding of the groupdel command and how you can use it to delete groups on a Linux-based operating system. There is not a huge amount to know about the command, and it is unlikely you will need to make use of it.

There are plenty more Linux commands that will make your life easier when it comes to administrating a Linux-based operating system such as Ubuntu. If you are after more user management commands, check out our tutorials on the useradd command, userdel command, and usermod command.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Leave a Reply

Your email address will not be published. Required fields are marked *