Delete orphaned UserProfiles

When you delete a user from the Sitefinity CMS users administration screen corresponding user profiles are also removed along with any associations. However, there are scenarios requiring you to integrate Sitefinity CMS with membership providers that persist and manage users outside of Sitefinity CMS – for example when you have integration with LDAP membership provider. In such scenarios users can be deleted directly from the external providers instead of using the Sitefinity CMS user administration screen. Sitefinity CMS ensures that users deleted form external providers can no longer login to your site but due to the delete operation happening outside of the Sitefinity CMS context, it cannot take care of any user profiles that have been associated with these users, and they can remain orphaned in the database.

You can work with the Sitefinity CMS API to identify orphaned profiles and delete them. The association between User and UserProfile is available in the User property of a UserProfile. This enables you to iterate through all user profiles and find the ones where User is null – this indicates that the user has been removed and this profile is orphaned. You can then proceed by deleting these profiles. Since you can configure Sitefinity CMS to work with more than one provider to manage UserProfile data, you can iterate through all available providers for the UserProfileManager. The following example demonstrates the process: 

NOTE: Iterating all UserProfiles and checking whether there is a User associated with them is a heavy operation. Consider running it asynchronously or when your website is not under heavy load.

If you want to clean orphaned user profiles just once or run it on-demand, you can integrate this sample in your custom implementation, for example inside a widget or an ASP. Net WebForm. In case you want to expose an endpoint, which can be called from an external provider or application you can also use the sample and add it to a WebService of your choice. For more information see: Tutorial: Implement a web service to delete orphaned user profiles

If you frequently delete users directly form the external providers your Sitefinity CMS is integrated with and want to automate the task of removing orphaned user profiles from the system you can use the Sitefinity CMS API to create a scheduled task and have it run or a specified interval. For more information see: For developers: Scheduled tasks

The following example demonstrates how you can use the sample provided earlier in this article inside a scheduled task:

NOTE: The task demonstrated above is not recurring. It will execute once at the specified DateTime point in time. If you want to create a recurring task, refer to: Tutorial: Recurring task - schedule a task to import POP3 content

To use the task in your Sitefinity CMS project you need to add the scheduled task sample from this article to your solution. You then need to schedule the task execution inside the Bootstrapper.Bootstrapped method in your Global.asax. To schedule the task execution call the ScheduleTask() method of the scheduled task, and specifying when to run the task as a DateTime value. Please note the DateTime value should be specified in UTC to ensure it’s aligned with the rest of the DateTime values persisted in the Sitefinity CMS database. The following sample demonstrates how you schedule the task to run in 1 hour:

 

Was this article helpful?