Delete users

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 For developers: Users.
  2. Get the user.
    Get an instance of the user. For more information, read For developers: Query 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 For developers: Query 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: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Telerik.Sitefinity.Security; using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users { public partial class ManagingUsersSnippets { 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 [For developers: Users](slug://for-developers-users).
2. Get all users, excluding the current one.  
    For more information, read [For developers: Query users](slug://for-developers-query-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 [For developers: Query user profiles](slug://for-developers-query-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:   ```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class ManagingUsersSnippets
    {
        public static void DeleteAllUsers(List<string> usernames)
        {
            UserManager userManager = UserManager.GetManager();
            UserProfileManager profileManager = UserProfileManager.GetManager();

            IQueryable<User> users = userManager.GetUsers().Where(u => u.UserName != SecurityManager.GetCurrentUserName());

            foreach (User user in users)
            {
                SitefinityProfile userProfile = profileManager.GetUserProfile<SitefinityProfile>(user);

                if (userProfile != null)
                {
                    profileManager.Delete(userProfile);
                }

                userManager.Delete(user);
            }

            profileManager.SaveChanges();
            userManager.SaveChanges();
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?