Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mary Greene 1 post 71 karma points
    Jul 21, 2020 @ 04:53
    Mary Greene
    0

    Sript to database Cleanup

    Hello,

    Is anyone aware of any C# or SQL scripts to perform a database cleanup? Looking to compact the data set of things like old log entries, unused data types, unused doc types, old/unused content versions, etc.omegle

    Regards

    Mary Greene

  • David Armitage 509 posts 2079 karma points
    Aug 04, 2020 @ 12:07
    David Armitage
    2

    Hi Mary,

    This has always worked great for Umbraco 7. Unfortunately I have not tested this on Umbraco 8 so please take a backup. My guess is it will probably work just fine.

    TRUNCATE TABLE umbracoLog 
    GO
    TRUNCATE TABLE umbracoUser2NodePermission
    GO
    TRUNCATE TABLE umbracoUserLogins
    GO
    
    -- Create a temporary table for all documents which are published and not in the recycle bin
    CREATE TABLE #Nodes (id int)
    GO
    -- Delete all rows if the table exists before
    TRUNCATE TABLE #Nodes
    GO
    
    -- Insert all nodeIds from all documents which are published and not in the recycle bin
    INSERT INTO #Nodes 
        SELECT N.id 
        FROM umbracoNode N
            INNER JOIN cmsDocument D ON N.ID = D.NodeId
        WHERE nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972'
            AND [path] NOT LIKE '%-20%'
            AND D.Published = 1
    GO
    
    -- Create a temporary table for all versionId's to delete
    CREATE TABLE #Versions (id UniqueIdentifier)
    GO
    -- Delete all rows if it exists before
    TRUNCATE TABLE #Versions
    GO
    
    -- Insert all versionId's from all nodeIds in the #Nodes table 
    -- and where published is set to false and newest is set to false
    INSERT INTO #Versions
        SELECT versionId 
        FROM cmsDocument 
        WHERE nodeId IN (SELECT id FROM #Nodes) AND published = 0 AND newest = 0
    GO
    
    -- DELETE all versions from cmsPreviewXml, cmsPropertyData, cmsContentVersion, cmsDocument
    -- from the nodes which are published and which are not in the recycle bin 
    -- and which are not published and which are not the newest
    DELETE FROM cmsPreviewXml WHERE versionId IN (SELECT id FROM #Versions)
    GO
    DELETE FROM cmsPropertyData WHERE VersionId IN (SELECT id FROM #Versions)
    GO
    DELETE FROM cmsContentVersion WHERE VersionId IN (SELECT id FROM #Versions)
    GO
    DELETE FROM cmsDocument WHERE VersionId IN (SELECT id FROM #Versions)
    GO
    
    -- Drop temp tables
    DROP TABLE #Versions
    GO
    DROP TABLE #Nodes
    GO
    
    -- Reindex tables  
    DBCC DBREINDEX (cmsPropertyData)
    DBCC DBREINDEX (cmsPreviewXml)
    DBCC DBREINDEX (cmsContentVersion)
    DBCC DBREINDEX (cmsDocument)
    DBCC DBREINDEX (cmsContentXml)
    DBCC DBREINDEX (umbracoDomains)
    DBCC DBREINDEX (umbracoUser2NodePermission)
    DBCC DBREINDEX (umbracoNode)
    DBCC DBREINDEX (cmsContent)
    
  • Paul 184 posts 646 karma points
    Nov 17, 2021 @ 13:04
    Paul
    0

    Thanks for the script @David Armitage, it's helping me shrink some v7 sites, just wondering, is

    TRUNCATE TABLE umbracoUser2NodePermission
    GO
    TRUNCATE TABLE umbracoUserLogins
    GO
    

    meant to be

    umbracoUserGroup2NodePermission (addition of Group) 
    and
    umbracoUserLogin (no s on the end)
    

    as I don't have tables called: umbracoUser2NodePermission or umbracoUserLogins

  • Mårten 3 posts 74 karma points
    Dec 05, 2021 @ 19:01
    MÃ¥rten
    0

    I know this is an old thread, but if anyone is interested I have been running this updated version of @David Armitage SQL on my v8 dev environment

    PRINT 'Clearing log- and audit tables'
    TRUNCATE TABLE umbracoLog
    GO
    TRUNCATE TABLE umbracoAudit
    GO
    TRUNCATE TABLE umbracoUserLogin
    GO
    
    PRINT 'Select all published document nodes'
    -- Create a temporary table for all documents which are published and not in the recycle bin
    DROP TABLE IF EXISTS #Nodes
    CREATE TABLE #Nodes (id int)
    GO
    
    -- Insert all nodeIds from all documents which are published and not in the recycle bin
    INSERT INTO #Nodes 
        SELECT N.id 
        FROM umbracoNode N
            INNER JOIN umbracoDocument D ON N.ID = D.NodeId
        WHERE nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972'
            AND [path] NOT LIKE '%-20%'
            AND D.Published = 1
    GO
    
    PRINT 'Select all unpublished document versions'
    -- Create a temporary table for all versionId's to delete
    DROP TABLE IF EXISTS #Versions
    CREATE TABLE #Versions (id int)
    GO
    
    -- Insert all versionId's from all nodeIds in the #Nodes table 
    -- and where published is set to false and [current] is set to false
    INSERT INTO #Versions
        SELECT CV.id 
        FROM umbracoDocumentVersion AS DV INNER JOIN umbracoContentVersion AS CV ON DV.id = CV.id
        WHERE nodeId IN (SELECT id FROM #Nodes) AND published = 0 AND [current] = 0
    GO
    
    PRINT 'DELETE unpublised versions from umbracoContentVersionCultureVariation, umbracoPropertyData, umbracoContentVersion, umbracoDocumentVersion '
    -- DELETE all versions from umbracoContentVersionCultureVariation, umbracoPropertyData, umbracoContentVersion, umbracoDocumentVersion
    -- from the nodes which are published and which are not in the recycle bin 
    -- and which are not published and which are not the newest
    DELETE FROM umbracoContentVersionCultureVariation WHERE versionId IN (SELECT id FROM #Versions)
    GO
    DELETE FROM umbracoPropertyData WHERE VersionId IN (SELECT id FROM #Versions)
    GO
    DELETE FROM umbracoDocumentVersion WHERE id IN (SELECT id FROM #Versions)
    GO
    DELETE FROM umbracoContentVersion WHERE id IN (SELECT id FROM #Versions)
    GO
    
    -- Drop temp tables
    DROP TABLE #Versions
    GO
    DROP TABLE #Nodes
    GO
    
    PRINT 'Reindex tables'
    -- Reindex tables  
    DBCC DBREINDEX (umbracoPropertyData)
    DBCC DBREINDEX (umbracoDocumentVersion)
    DBCC DBREINDEX (umbracoContentVersion)
    DBCC DBREINDEX (umbracoDocument)
    DBCC DBREINDEX (umbracoContent)
    DBCC DBREINDEX (umbracoContentVersionCultureVariation)
    DBCC DBREINDEX (umbracoDomain)
    DBCC DBREINDEX (umbracoAudit)
    DBCC DBREINDEX (umbracoNode)
    GO
    
    PRINT 'Shrink databse'
    DBCC SHRINKDATABASE (0)
    GO
    
  • Potcoava Mihai 5 posts 26 karma points
    Nov 13, 2020 @ 06:33
    Potcoava Mihai
    0

    Hello,

    did anyone test the above script in Umbraco 8?

    Many thanks, Mihai.

Please Sign in or register to post replies

Write your reply to:

Draft