We have a bucketload of team sites that are created using custom code and these are constantly being created/updated/deleted.

Our security requirements mean that these sites must have unique permissions with associated owner/member/visitor groups.

When the site is deleted, however, the associated groups are left hanging around and have to be deleted manually. This doesn't happen if we create the team site through the normal sharepoint web interface.

Is there any way to make it so that our custom created groups are deleted when the site is?

We're using the following code to add the groups:

                //Create the owners group
                newSite.SiteGroups.Add(ownersName, owner, owner, siteName + " Owners Group");
                SPGroup ownersGroup = newSite.SiteGroups[ownersName];
                ownersGroup.AllowMembersEditMembership = true;
                newSite.AssociatedOwnerGroup = ownersGroup;

                //Add RoleAssignments and RoleDefinitions
                SPRoleAssignment ownerAssignment = new SPRoleAssignment(ownersGroup);
                SPRoleDefinition ownerDefinition = newSite.RoleDefinitions["Full Control"];

1 Answer 1


I'm assuming that these are subsites?

You'll need to create an event listener on the Site Collection, watching for web deletion events. At that point, you'll be able to delete the appropriate groups using the context object the listener handler gives you.

  • Thanks. That seems a bit roundabout though. How come the standard sharepoint team sites get their groups deleted by default?
    – Anonymous
    Apr 30, 2010 at 7:27
  • Open up the code for the deleteweb.aspx page, and see what Microsoft is doing - you may be surprised how complex some of the underlying code is (though maybe not this - I haven't looked). Apr 30, 2010 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.