In an earlier article, I discussed the groupadd command which is used for adding new groups to your Linux system.
In this article, I’ll discuss another group management command which is groupdel. It is used for deleting groups from your system.
Using groupdel command in Linux
All you have to do is to specify the group name. The group must exist, of course.
sudo groupdel group_name
Did you notice that I used sudo with the groupdel command? It’s because it is an administrative command and you need to be root or sudo user to run this command.
Keep in mind: You cannot delete primary group of a user without deleting the user
If you try to remove the primary group of an existing user, you should see an error like this:
groupdel: cannot remove the primary group of user ‘abhishek’
This is by design. You cannot delete the primary group of an existing user. You have two options here:
- Change the primary group of a user using usermod command
- Delete the user using userdel command and then delete the group
Keep in mind: Files belonging to the deleted group will not change group ownership
The groupdel command doesn’t change the group ownership of any files. It simply removes the group name from the system. If you list files owned by a deleted group, you’ll see the group id (GID) instead of group name.
I hope you are aware of the file permission and ownership concept in Linux. As you can see below, the file belongs to group tesla:
[email protected]:~$ ls -l a.txt -rw-r--r-- 1 abhishek tesla 57 Feb 24 15:57 a.txt
Once I delete the group, the file shows the group ID (998 in the example) instead of the group name:
[email protected]:~$ sudo groupdel tesla [email protected]:~$ ls -l new.txt -rw-r--r-- 1 abhishek 998 1717 Feb 24 15:58 new.txt
What do you do with such files, then? It’s up to you really. You can leave it like that or use a combination of find, xargs and chgrp command to change the group of files belonging to the GID of the deleted group like this:
find . -gid 998 | xargs chgrp another_group_name
Since we are speaking of file permissions and ownership, you might want to know about the stat command that can display these information in detail.
I hope this tutorial gave you a better understanding of the groupdel command in Linux. If you have any questions or suggestions, please let me know in the comments.