groupdel Command in Linux: Delete a Group

By 

Updated on

5 min read

groupdel Command in Linux

In Linux, groups are used to organize and administer user accounts. The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permissions for a given resource that can be shared among the users within the group.

A new group can be created using the groupadd command. If a group is no longer needed, it can be removed from the system using the groupdel command.

This article explains how to remove a group in Linux using the groupdel command.

groupdel Command Syntax

The general syntax for the groupdel command is as follows:

txt
groupdel GROUPNAME

GROUPNAME is the name of the group you want to remove. Only root or a user with sudo privileges can remove groups.

Before Deleting a Group

Before removing a group, it is worth checking a few things.

Check whether any users have the group as their primary group. It is not possible to remove a group that is the primary group of an existing user. To find which users have a specific group as their primary group, first get the group’s GID:

Terminal
getent group mygroup
output
mygroup:x:1005:

Then search for users with that GID in /etc/passwd:

Terminal
awk -F: '$4 == 1005 {print $1}' /etc/passwd

If any users are returned, you must either change their primary group with usermod or remove those users before you can delete the group.

Check and remove group members. To see the current members of a group:

Terminal
getent group mygroup

To remove a user from the group before deleting it:

Terminal
sudo gpasswd -d username mygroup

Deleting a Group in Linux

To delete a group, run groupdel followed by the group name:

Terminal
sudo groupdel mygroup

The command removes the group entry from the /etc/group and /etc/gshadow files. On success, no output is printed.

To verify that the group has been removed, use getent :

Terminal
getent group mygroup

If the group was successfully deleted, the command returns no output.

What Happens to Files Owned by the Deleted Group

Deleting a group does not delete or modify any files owned by that group. Files previously owned by the group retain their numeric GID. When you list such files, the GID appears as a number instead of a group name:

Terminal
ls -la /path/to/file
output
-rw-r--r-- 1 linuxize 1005 1024 Mar 01 10:00 file.txt

If the GID is later assigned to a new group, those files will appear to be owned by the new group, which can be a security concern. To find all files still owned by the old GID after deletion:

Terminal
sudo find / -gid 1005

On large systems, scanning from / can take time. If you already know where the files are, limit the search scope (for example, /home or /srv) to speed up the audit.

Review the results and reassign ownership as needed using chown:

Terminal
sudo chown :newgroup /path/to/file

Troubleshooting

groupdel: cannot remove the primary group of user 'username' The group is set as the primary group of an existing user. Change the user’s primary group first with sudo usermod -g newgroup username, then retry the deletion.

groupdel: group 'mygroup' does not exist The group name is misspelled or the group has already been removed. Run getent group mygroup to confirm whether it exists.

Files still show a numeric GID after group deletion The files retain the old GID. The group name no longer exists in /etc/group so the system displays the raw number. Use sudo find / -gid GID to locate affected files and chown :newgroup file to reassign ownership.

Quick Reference

CommandDescription
sudo groupdel GROUPDelete a group
getent group GROUPCheck if a group exists and view its GID and members
awk -F: '$4 == GID' /etc/passwdFind users with a specific primary GID
sudo gpasswd -d USER GROUPRemove a user from a group before deletion
sudo find / -gid GIDFind files still owned by a deleted group’s GID

FAQ

What happens to files owned by a deleted group? The files are not deleted or changed. They retain the numeric GID of the deleted group. If that GID is later reassigned to a new group, the files will appear owned by the new group. Always audit and reassign file ownership after deleting a group.

How do I check which users belong to a group before deleting it? Run getent group groupname. The last field lists the supplementary members. To find users who have the group as their primary group, match the GID against /etc/passwd using awk -F: '$4 == GID {print $1}' /etc/passwd. See How to List Users in Linux for more options.

Can I delete a group that still has members? Yes, as long as the group is not the primary group of any existing user. Supplementary group members are not blocked from deletion. However, it is good practice to remove members first with gpasswd -d to avoid stale group references. See How to Add a User to a Group for managing group membership.

Conclusion

Use groupdel to remove a group from the system. Before deleting, verify that no user has the group as a primary group, and check for files still owned by the group’s GID after deletion.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page