Bulk deleting members and their property data
r
Hi, I'm looking to create some reusable code to bulk delete members and their property data from a site's database. The most straightforward way would be to use the member service, but I'd imagine that could be rather inefficient with thousands of members. That's also assuming the delete method also removes the versions of the property data which is what I want. The more efficient way I can think of is to write some SQL statements to clear out the various tables. Has anybody got any code that already does this before I write it? Does anybody have any other suggestions on ways to achieve this? Thanks for your time.
g
Hi, I needed this today and I wrote this:
Copy code
-- Start a transaction to ensure all or none of the deletes occur
BEGIN TRANSACTION;

-- Define a temp table to hold the nodeId values from the cmsMember table
CREATE TABLE #nodeIds (nodeId INT);

-- Insert the nodeId values from the cmsMember table
INSERT INTO #nodeIds (nodeId)
SELECT nodeId FROM cmsMember;


-- Step 1: Delete from cmsMember2MemberGroup where nodeId matches
DELETE FROM cmsMember2MemberGroup
WHERE Member IN (SELECT nodeId FROM #nodeIds);

-- Step 2: Delete from umbracoContent where nodeId matches
DELETE FROM umbracoContent
WHERE nodeId IN (SELECT nodeId FROM #nodeIds);

-- Step 3: Delete from cmsMember where nodeId matches
DELETE FROM cmsMember
WHERE nodeId IN (SELECT nodeId FROM #nodeIds);


-- Step 4: Delete from cmsContentNu where nodeId matches
DELETE FROM cmsContentNu
WHERE nodeId IN (SELECT nodeId FROM #nodeIds);


-- Step 5: Delete from umbracoNode where nodeId matches
DELETE FROM umbracoNode
WHERE Id IN (SELECT nodeId FROM #nodeIds);



-- Drop the temporary table
DROP TABLE #nodeIds;

-- Commit the transaction if everything went fine
COMMIT TRANSACTION;
d
Doesn’t this miss or orphan all the records in UmbracoPropertyData for those members?
11 Views