SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an SPGroup object populated, how can I remove all members from the group, so that essentially I start with a fresh group.

I can't delete and recreate it, so I need a way to work with the existing SharePoint group.

Thanks in advance

share|improve this question
up vote 10 down vote accepted

Take a look on msdn

    foreach (SPUser user in group.Users)
    {
        group.RemoveUser(user);
    }

But be aware that you change this group for the whole site collection, if you have other site collections those won't be impacted by the modification of this group.

share|improve this answer
2  
Won't this throw a modify collection exception? – JL01 Sep 16 '11 at 11:55
    
I have tested it and it works. – Anatoly Mironov Sep 16 '11 at 12:18
1  
Neat, was looking for this. I was afraid that this would in fact skip every other user due to the indexes changing as users get removed, as is the situation when removing groups for example. Tried it, and it worked fine. Nice solution Anatoly. =) – Johannes Jan 31 '12 at 16:12
    
I'm not sure how this works since the collection is being modified directly. Is it because this is server code and it gets a new instance of the collection during each iteration? I know the CSOM version of this would not work. – Dinerdo Apr 13 at 20:05
   while (group.Users.Count > 0)
     {
        group.Users.Remove(group.Users[0]);
     }

This should remove all the users in the group for CSOM and SSOM.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.