-
-
Save tomvanenckevort/40d53193229a040ec87f19d7e83feb49 to your computer and use it in GitHub Desktop.
Umbraco v7 Database cleanup. After pulling in an umbraco database from production, you don't need all history or log.
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
-- 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()) | |
-- dump logs | |
-- TRUNCATE TABLE umbracolog -- faster if log table is very big and you don't need anything | |
DELETE FROM umbracolog WHERE Datestamp < @createdDate | |
-- clean up old versions | |
DELETE FROM cmsPropertyData WHERE | |
versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > @createdDate OR published = 1 OR newest = 1) AND | |
contentNodeId IN (SELECT DISTINCT nodeID FROM cmsDocument) | |
DELETE FROM cmsPreviewXml WHERE | |
versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > @createdDate OR published = 1 OR newest = 1) AND | |
nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument) | |
DELETE FROM cmsContentVersion WHERE | |
versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > @createdDate OR published = 1 OR newest = 1) AND | |
ContentId IN (SELECT DISTINCT nodeID FROM cmsDocument) | |
DELETE FROM cmsDocument WHERE | |
versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > @createdDate OR published = 1 OR newest = 1) AND | |
nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument) | |
--http://blog.dampee.be/post/2015/11/28/Remove-Old-versions-of-Umbraco-Previews.aspx | |
delete | |
--select * | |
from cmsPreviewXml | |
where versionId in ( | |
select cmsPreviewXml.versionId | |
from cmsPreviewXml join cmsDocument on cmsPreviewXml.versionId=cmsDocument.versionId | |
where cmsDocument.newest <> 1) | |
-- set the database to simple recovery (IMPORTANT: don't do this on a live site!) | |
ALTER DATABASE CURRENT SET RECOVERY SIMPLE; | |
-- shrink the database files | |
CHECKPOINT; | |
GO | |
CHECKPOINT; -- run twice to ensure file wrap-around | |
GO | |
DBCC SHRINKDATABASE(0); | |
GO | |
DECLARE @dbName VARCHAR(100) | |
SELECT @dbName = name FROM sys.master_files WHERE database_id = db_id() AND type = 0 | |
DBCC SHRINKFILE(@dbName, 0, TRUNCATEONLY); | |
GO | |
DECLARE @logName VARCHAR(100) | |
SELECT @logName = name FROM sys.master_files WHERE database_id = db_id() AND type = 1 | |
DBCC SHRINKFILE(@logName, 200); -- unit is set in MBs | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment