-
-
Save pedroadaodev/7709f7c1f1330d0dd2bebb558659ecda to your computer and use it in GitHub Desktop.
Umbraco Database cleanup. After pulling in an umbraco database from production, you don't need all history or log.
This file contains 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
-- Umbraco Clear Old Document Versions To Decrease Database Size And Improve Performance | |
-- http://borism.net/2008/12/16/fixing-a-large-cmspropertydata-table-in-umbraco/ | |
--DECLARE @createdDate Datetime = DATEADD(m, -1, getdate()) -- If you want to get 2 weeks without maintenance - 40 minutes to run | |
DECLARE @createdDate Datetime = DATEADD(day, -10, getdate()) -- If you want to get 3/4 weeks without maintenance - 60 minutes to run | |
--select @createdDate | |
-- dump logs | |
-- TRUNCATE TABLE umbracolog -- faster if log table is very big and you don't need anything | |
DELETE FROM umbracolog WHERE Datestamp < @createdDate | |
print 'deleted umbracologs' | |
SELECT versionId | |
INTO #TempVersionIds | |
FROM cmsDocument WHERE updateDate > @createdDate OR published = 1 OR newest = 1; | |
print 'selected #TempVersionIds' | |
SELECT DISTINCT nodeID | |
INTO #TempNodeIds | |
FROM cmsDocument | |
AS TempNodeIds; | |
print 'selected #TempNodeIds' | |
SELECT cmsPreviewXml.versionId as versionId | |
INTO #TempcmsPreviewXmlVersionId | |
from cmsPreviewXml join cmsDocument on cmsPreviewXml.versionId=cmsDocument.versionId | |
where cmsDocument.newest <> 1 | |
print 'selected #TempcmsPreviewXmlVersionId' | |
-- clean up old versions | |
DELETE FROM cmsPropertyData WHERE versionId NOT IN (SELECT versionId FROM #TempVersionIds) AND contentNodeId IN (SELECT nodeID FROM #TempNodeIds) | |
print 'deleted cmsPropertyData' | |
DELETE FROM cmsPreviewXml WHERE versionId NOT IN (SELECT versionId FROM #TempVersionIds) AND nodeId IN (SELECT nodeID FROM #TempNodeIds) | |
print 'deleted cmsPreviewXml' | |
DELETE FROM cmsContentVersion WHERE versionId NOT IN (SELECT versionId FROM #TempVersionIds) AND ContentId IN (SELECT nodeID FROM #TempNodeIds) | |
print 'deleted cmsContentVersion' | |
DELETE FROM cmsDocument WHERE versionId NOT IN (SELECT versionId FROM #TempVersionIds) AND nodeId IN (SELECT nodeID FROM #TempNodeIds) | |
print 'deleted cmsDocument' | |
--http://blog.dampee.be/post/2015/11/28/Remove-Old-versions-of-Umbraco-Previews.aspx | |
delete | |
--select * | |
from cmsPreviewXml | |
where versionId in (select versionId from #TempcmsPreviewXmlVersionId) | |
print 'deleted cmsPreviewXml' | |
Drop Table #TempVersionIds | |
Drop Table #TempNodeIds | |
Drop Table #TempcmsPreviewXmlVersionId | |
print 'droped temp tables' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the original script to use TEMP Tables