Deleting users in CRM Dynamics 2011

Joris Van Regemortel picture Joris Van Regemortel · Jun 17, 2013 · Viewed 9.3k times · Source

I'm fully aware that deleting users (SystemUser Entity) in CRM Dynamics 2011 is not supported by Microsoft.

However, we're currently developing a tool to support our User Provisioning needs. In order to be able to write integration tests for this tool, it seems necessary to be able to remove users afterwards, so that we can rollback our test environment to the original state.
Currently, we're doing this by restoring organizations from SQL backups, but this is too time consuming to do for each test run.

Update

So far, the best solution we have is to create a user in the integration test, assert everything we need to assert, and afterwards "clean it up" by disabling the user and removing its AD credentials, so that we can re-use those credentials for the next run of the test.

However, as we're only looking for a solution for a test environment, I would really like to have a solution that cleans everything up properly: deleting the records in SQL seems like the way to go. Due to the complex DB structure, however, I was hoping that someone could provide scripts for this.

Update2

We've created the script to manually delete the user from SQL (see accepted answer). This is not supported, so only use it in test environments, if you know what you're doing.

Answer

Joris Van Regemortel picture Joris Van Regemortel · Jun 20, 2013

The following script is not supported by Microsoft. Using it might harm, brick, blow up or molest your CRM organization, deployment, server and career.
Never use this.
Ever.

That being said, we used this, and it worked fine for our purpose: cleaning up our test environment after running AddSystemUser tests.

Some other things to keep in mind :

  • We are using CRM Dynamics 2011 UR10 On-Premises
  • Our test user doesn't have any related records, it's just an isolated user
  • We're using AD authentication
USE OrganizationName_MSCRM
BEGIN TRANSACTION
DECLARE @username AS VARCHAR(50)

-- CHANGE THIS -- 
SET @username = 'domain\username'
-- DONT CHANGE ANYTHING AFTER THIS --

DECLARE @userId AS UNIQUEIDENTIFIER
SET @userId = (SELECT SystemUserId  FROM dbo.SystemUserBase WHERE DomainName = @username)

DECLARE @orgid AS UNIQUEIDENTIFIER
SET @orgid = (SELECT OrganizationId FROM dbo.SystemUserBase WHERE systemuserid = @userid)

DECLARE @userEmail AS VARCHAR(MAX)
SET @useremail = (SELECT InternalEMailAddress FROM dbo.SystemUserBase WHERE SystemUserId = @userid)

DECLARE @userfullname AS VARCHAR(max)
SET @userfullname = (SELECT fullname FROM dbo.systemuserbase WHERE systemuserid = @userid)

DECLARE @queueid AS UNIQUEIDENTIFIER
SET @queueid = (SELECT queueid FROM dbo.SystemUserBase WHERE SystemUserId = @userid)

DECLARE @ownerid AS UNIQUEIDENTIFIER
SET @ownerid = (SELECT ownerid FROM dbo.OwnerBase WHERE name = @userfullname)

DELETE FROM dbo.SystemUserExtensionBase WHERE SystemUserId = @userId
DELETE FROM dbo.UserSettingsBase WHERE SystemUserId = @userId
DELETE FROM dbo.TeamMembership WHERE SystemUserId = @userId
DELETE FROM dbo.SystemUserPrincipals WHERE systemuserid = @userId
DELETE FROM dbo.SystemUserRoles WHERE systemuserid = @userId
DELETE FROM dbo.SystemUserBusinessUnitEntityMap WHERE systemuserid = @userid
DELETE FROM dbo.UserQueryBase WHERE OwnerId = @userid
DELETE FROM dbo.SystemUserProfiles WHERE SystemUserId = @userId
DELETE FROM dbo.SystemUserBase WHERE SystemUserId = @userid
DELETE FROM dbo.QueueBase WHERE QueueId = @queueid
DELETE FROM dbo.PrincipalEntityMap WHERE PrincipalId = @ownerid
DELETE FROM dbo.PrincipalObjectAccess WHERE principalid = @ownerid
DELETE FROM dbo.OwnerBase WHERE ownerid = @ownerid
DELETE FROM dbo.EmailSearchBase WHERE EmailAddress = @userEmail
DELETE FROM dbo.ResourceBase WHERE name = @userfullname
DELETE FROM dbo.CalendarRuleBase WHERE CalendarId IN (SELECT CalendarId FROM dbo.CalendarBase WHERE PrimaryUserId = @userid)
DELETE FROM dbo.CalendarBase WHERE primaryuserid = @userId
DELETE FROM dbo.InternalAddressBase WHERE parentid = @userId

DELETE FROM mscrm_config..SystemUserOrganizations WHERE CrmUserId = @userid AND OrganizationId = @orgid

COMMIT