Deleting users

This topic contains examples of how to:

Deleting a single user

To delete a specific user you must perform the following:

  1. Get instance of the user manager.
    The manager is represented by the UserManager class. For more information about getting the instance, see Managing users.
  2. Get the user.
    Get an instance of the user. For more information, read Querying users.
  3. Get the user profile.
    You must also delete any existing profiles for the specified user. Note that if you have more than one profile type for each user, you must get all the profiles related to the user and delete them. For more information, read Querying user profiles.
  4. Mark the user and the user profile for deletion.
    To mark the user and the user profile to be deleted, you call the Delete method of the respective manager.
  5. Save changes.
    To actually delete  the user and the user profile, you must save the changes to the managers.

Here is a code example:

public static void DeleteUser(string username)
{
    UserManager userManager = UserManager.GetManager();
    UserProfileManager profileManager = UserProfileManager.GetManager();
 
    User user = userManager.GetUsers().Where(u => u.UserName == username).SingleOrDefault();
 
    if (user != null)
    {
        SitefinityProfile userProfile = profileManager.GetUserProfile<SitefinityProfile>(user);
 
        if (userProfile != null)
        {
            profileManager.Delete(userProfile);
        }
 
        userManager.Delete(user);
    }
 
    profileManager.SaveChanges();
    userManager.SaveChanges();
}

Deleting all users

To delete all users, you must perform the following:

  1. Get instance of the user manager.
    The manager is represented by the UserManager class. For more information about getting the instance, see Managing users.
  2. Get all users.
    For more information, read Querying users.
  3. Iterate through the users collection.
  4. Get the user profile.
    You must also delete any existing profiles for the specified user. Note that if you have more than one profile type for each user, you must get all the profiles related to the user and delete them. For more information, read Querying user profiles.
  5. Mark the user and the user profile for deletion.
    To mark the user and the user profile to be deleted, you call the Delete method of the respective manager.
  6. Save changes.
    To actually delete the user and the user profile, you must save the changes to the managers.

Here is a code example:

public static void DeleteAllUsers()
{
    UserManager userManager = UserManager.GetManager();
    UserProfileManager profileManager = UserProfileManager.GetManager();
 
    IQueryable<User> users = userManager.GetUsers();
 
    foreach (User user in users)
    {
        SitefinityProfile userProfile = profileManager.GetUserProfile<SitefinityProfile>(user);
 
        if (userProfile != null)
        {
            profileManager.Delete(userProfile);
        }
 
        userManager.Delete(user);
    }
 
    profileManager.SaveChanges();
    userManager.SaveChanges();
}

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK