Using the below code we can remove SharePoint group from the site. RemoveSPGroup is like generic method, which accepts parameters as SPSite object, and Group name then deletes the group from site.
code:
//Removes Group from site
public void RemoveSPGroup(SPSite site,string groupName)
{
using (SPWeb oWebsiteRoot = site.RootWeb)
{
if (CheckGroupExistsInSiteCollection(oWebsiteRoot, groupName))
{
SPGroupCollection siteGroups = oWebsiteRoot.SiteGroups;
siteGroups.Remove(groupName);
}
}
}
//checks group exists in site collection
private static bool CheckGroupExistsInSiteCollection(SPWeb web, string groupName)
{
return web.SiteGroups.OfType<SPGroup>().Count(g => g.Name.Equals(groupName, StringComparison.InvariantCultureIgnoreCase)) > 0;
}
Method call:
SPSite currentSite = SPContext.Current.Site;
RemoveSPGroup(currentSite,"Group Visitors")
Output:
Removes "Group Visitors" from site