groupdel Command Cheat Sheet
The groupdel command modifies the system account files (/etc/group and /etc/gshadow) to delete all entries that refer to the specified group.
Synopsis
groupdel [options] GROUP
Basic Usage
Delete a Group
sudo groupdel developers
This removes the group developers. Note that it does not delete files owned by this group; those files will now be owned by a "numbered" GID (orphaned GID).
Restrictions
Cannot Delete User's Primary Group
You cannot remove the primary group of any existing user.
sudo groupdel faruk
# Output: groupdel: cannot remove the primary group of user 'faruk'
Solution: 1. Change the user's primary group first:
sudo usermod -g users faruk
sudo userdel faruk
Troubleshooting
"groupdel: group 'xyz' does not exist"
You are trying to delete a group that isn't there. Verify with:
getent group xyz
"groupdel: cannot open /etc/group"
You likely forgot sudo.
Orphaned Files Cleanup
When a group is deleted, files owned by that group retain the old GID.
specific GID
If you deleted devs (GID 1005), find files left behind:
find / -gid 1005
Fix Orphaned Files
Change their ownership to a new group (e.g., root or users).
find /home -gid 1005 -exec chgrp users {} \;
Forced Deletion (-f)
⚠️ dangerous: This option causes groupdel to proceed even if the group is the primary group of a user.
sudo groupdel -f legacy_group
ls -l will show numbers instead of names. Not recommended.
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 2 | Invalid command syntax |
| 6 | Specified group doesn't exist |
| 8 | Can't remove user's primary group |
| 10 | Can't update group file |
Verification
Check if deletion was successful.
if ! getent group developers > /dev/null; then
echo "Group successfully deleted"
fi
Notes
- Password & Shadow:
groupdelautomatically cleans up entries from/etc/gshadowif it exists. - NIST/Security: In high-security environments, ensure you audit files owned by the deleted group to prevent "unowned" files from being claimed by a new group created later with the same GID.