Delete users
Deleting a single user
To delete a specific user you must perform the following:
- Get instance of the user manager.
The manager is represented by theUserManagerclass. For more information about getting the instance, see For developers: Users. - Get the user.
Get an instance of the user. For more information, read For developers: Query users. - 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. - Mark the user and the user profile for deletion.
To mark the user and the user profile to be deleted, you call theDeletemethod of the respective manager. - 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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.