groupdel Command in Linux (original) (raw)

Last Updated : 11 May, 2026

The groupdel command in Linux is used to delete an existing group from the system. It removes the group entry from system group files, but does not delete users belonging to that group. This command is commonly used during cleanup, project removal, or restructuring of user permissions.

Example 1: Delete an Existing Group

**Syntax:

sudo groupdel group_name

**Command:

sudo groupdel testgrp1

**Output:

groupdel-ex1

**Verify Deletion:

grep testgrp1 /etc/group

**Output:

Example 2: Attempt to Delete Non-Existing Group

**Command:

sudo groupdel nonexistinggroup

**Output:

groupdel-ex2

**Notes: Helps identify incorrect group names.

Example 3: Delete Group Used as Primary Group

If a group is set as the primary group of a user:

**Command:

sudo groupdel primarygrp

**Check exit code:

echo $?

**Output:

groupdel-ex3

**Notes:

**Syntax of `groupdel` Command

groupdel [options] group_name

**Files Used:

**Exit values:

groupdel command exits with the following status codes:

Options of groupdel Command

1. -h, --help

Displays the help message for the groupdel command and exits. It provides usage syntax and a list of available options. This option does not modify the system.

**Syntax:

groupdel --help

**Output:

groupdel_help

2. -R, --root CHROOT_DIR

Deletes a group inside a specified chroot environment. Instead of modifying the current system’s /etc/group, it operates inside the provided directory. This is commonly used when repairing or managing another Linux installation mounted inside a directory.

**Syntax:

sudo groupdel -R CHROOT_DIR group_name

**Example: Deleting a Group Inside a Chroot Environment

Deletes a group named developers from a mounted Linux system located at /mnt/chroot.

**Command:

sudo groupdel -R /mnt/chroot developers

**Output:

**Verification:

grep developers /mnt/chroot/etc/group

3. -P, --prefix PREFIX_DIR

Specifies a directory prefix where /etc/* files are located. Unlike -R, this does not perform a full chroot, but simply works relative to the given directory. Useful when managing system images or container filesystems.

**Syntax:

sudo groupdel -P PREFIX_DIR group_name

**Example: Deleting a Group Using Custom Prefix Directory

Deletes the group developers from a system image stored in /mnt/customroot.

**Command:

sudo groupdel -P /mnt/customroot developers

**Output:

**Verification:

grep developers /mnt/customroot/etc/group

4. -f, --force

Forces deletion of a group even if it is the primary group of an existing user. Normally, groupdel prevents deleting such groups to protect user configuration. This option overrides that protection.

**Syntax:

sudo groupdel -f group_name

**Example: Force Deleting a Primary Group

Deletes a group named alice even if it is set as the primary group of a user.

sudo groupdel -f alice

**Output:

**Without -f, You Would be:

groupdel: cannot remove the primary group of user 'alice'

**Notes: May cause user account inconsistencies. Recommended only in controlled environments.

Deletes a group from the extra users database instead of /etc/group. Used in systems configured with libnss-extrausers.

**Group data is stored in:

/var/lib/extrausers/group

**Syntax:

sudo groupdel --extrausers group_name

**Example: Deleting a Group from Extra Users Database

Deletes the group tempgroup from the extra users database.

sudo groupdel --extrausers tempgroup

**Output:

**Verification:

grep tempgroup /var/lib/extrausers/group

**Notes: