Last active
March 9, 2017 19:53
-
-
Save timgaunt/6fac01c3c82d83b9350a48d84d4ae649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE | |
@RowsToDelete INT = 100 | |
, @rows INT = 1 | |
, @batch INT = 1 | |
, @batchestorun INT = 2000 | |
, @message nvarchar(max) | |
, @keepOldVersionsCount int | |
, @keepNewerThanDate DATETIME | |
SET @keepOldVersionsCount = 0 /* 0 Keeps published and newest only. */ | |
SET @keepNewerThanDate = GETDATE() /* GETDATE() or '2013-01-01' */ | |
WHILE @rows > 0 | |
BEGIN | |
IF OBJECT_ID('tempdb..#Nodes') IS NOT NULL DROP TABLE #Nodes | |
SELECT TOP (@RowsToDelete) n.id AS NodeId INTO #Nodes | |
FROM umbracoNode n | |
WHERE n.path like '%-20%' and id!=-20 | |
ORDER BY n.path DESC | |
SELECT * FROM #Nodes | |
--delete from cmsPreviewXml where nodeId in (select NodeId from #Nodes) | |
--delete from cmsContentVersion where contentId in (select NodeId from #Nodes) | |
--delete from cmsDocument where nodeId in (select NodeId from #Nodes) | |
--delete from cmsContentXML where nodeId in (select NodeId from #Nodes) | |
--delete from cmsContent where nodeId in (select NodeId from #Nodes) | |
--delete from cmsPropertyData where contentNodeId in (select NodeId from #Nodes) | |
--delete from umbracoRelation where parentId in (select NodeId from #Nodes) OR childId in (select NodeId from #Nodes) | |
--delete from cmsTagRelationship where nodeId in (select NodeId from #Nodes) | |
--delete FROM umbracoUser2NodePermission WHERE nodeId in (select NodeId from #Nodes) | |
--DELETE FROM umbracoDomains WHERE domainRootStructureID in (select NodeId from #Nodes) | |
--delete FROM umbracoRedirectUrl WHERE contentKey IN (select uniqueID from #Nodes n INNER JOIN umbracoNode un ON n.NodeId=un.id) | |
--delete from umbracoNode where id in (select NodeId from #Nodes) | |
SET @rows = @@ROWCOUNT; | |
SET @message = (CONVERT( VARCHAR(24), GETDATE(), 121)) + ' Deleted batch: ' + CAST((@batch) AS NVARCHAR) + ' (' + CAST((@rows) AS NVARCHAR) + ' rows affected)' | |
RAISERROR(@message,0,1) WITH NOWAIT | |
END | |
SET @rows = 1 | |
WHILE @rows > 0 AND @batch <= @batchestorun | |
BEGIN | |
IF OBJECT_ID('tempdb..#versions') IS NOT NULL DROP TABLE #versions | |
SELECT TOP (@RowsToDelete) VersionID, nodeId, updateDate, newest, published INTO #versions | |
FROM cmsDocument | |
WHERE versionID NOT IN | |
(SELECT D.versionId | |
FROM cmsDocument D | |
WHERE D.versionId IN | |
(SELECT versionId | |
FROM | |
(SELECT CV.versionId, published, newest, CV.versionDate, RANK() OVER(ORDER BY CV.versionDate DESC) RowNum | |
FROM cmsContentVersion CV JOIN cmsDocument DD ON CV.versionId = DD.versionId | |
WHERE DD.nodeId = D.nodeId) AS tmp | |
WHERE tmp.RowNum <= (@keepOldVersionsCount + 2) OR tmp.published = 1 OR tmp.newest = 1 or tmp.versionDate >= @keepNewerThanDate) | |
) | |
SET @rows = @@ROWCOUNT; | |
SELECT * FROM #versions | |
--DELETE FROM cmsPreviewXml WHERE versionId IN (select #versions.VersionId FROM #versions) | |
--DELETE FROM cmsContentVersion WHERE VersionId IN (select #versions.VersionId FROM #versions) | |
--DELETE FROM cmsPropertyData WHERE VersionId IN (select #versions.VersionId FROM #versions) | |
--DELETE FROM cmsDocument WHERE VersionId IN (select #versions.VersionId FROM #versions) | |
SET @message = (CONVERT( VARCHAR(24), GETDATE(), 121)) + ' Done with batch: ' + CAST((@batch) AS NVARCHAR) + ' (' + CAST((@rows) AS NVARCHAR) + ' rows affected)' | |
RAISERROR(@message,0,1) WITH NOWAIT | |
SET @batch = @batch + 1; | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added umbracoDomains which now have relations to node?