How to delete all user profiles programatically

Sharepoint interface does not provide any functionality to clear imported profiles, you can do it in batches of 50, if you have more than 1000 profiles it will not look good on your timesheet ;)   Furthermore, sometimes you bring your profile service to the state that you cannot even retrieve any profile even they are there (I will one day write KB about that because it is very interesting issue related to NETBIOS domain name)
So this is a code to delete all profiles:

[sourcecode language="csharp"]
System.Collections.IEnumerator enumProfs = profileManager.GetEnumerator();
enumProfs.Reset();
bool continueEnum = true;

// loop through profiles
while (continueEnum)
{
try
{
// move to next profile
continueEnum = enumProfs.MoveNext();
}
catch (Exception ex)
{
continueEnum = enumProfs.MoveNext();
}
UserProfile currentProfile = (UserProfile)enumProfs.Current;
profileManager.RemoveUserProfile(currentProfile.ID);
}[/sourcecode]

Leave a Reply