Last active
April 30, 2019 09:51
-
-
Save adnanzameer/de982b7b0fb2aa2eadf22c599cae52e0 to your computer and use it in GitHub Desktop.
SQL script to cleanup Episerver old content versions
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
Declare @VersionsToKeep int = 4 | |
Declare @PublishedVersions int = 1 | |
declare @fkPageID int; | |
declare db_cursor CURSOR FOR select fkPageID from tblWorkpage where HasBeenPublished= @PublishedVersions Group by fkpageid having count(fkPageID) > @PublishedVersions order by count(fkPageID) desc | |
open db_cursor | |
fetch next from db_cursor into @fkPageID | |
while @@FETCH_STATUS = 0 | |
BEGIN | |
declare @languageID int; | |
declare language_cursor CURSOR FOR select fkLanguageBranchID FROM tblWorkPage WHERE fkPageID= @fkPageID GROUP BY fkLanguageBranchID | |
open language_cursor | |
fetch next from language_cursor into @languageID | |
while @@FETCH_STATUS = 0 | |
BEGIN | |
DECLARE @ObsoleteVersions INT | |
SELECT @ObsoleteVersions= count(fkPageID) from tblWorkpage WHERE fkPageID= @fkPageID AND HasBeenPublished= @PublishedVersions AND fkLanguageBranchID = @languageID | |
WHILE (@ObsoleteVersions > @VersionsToKeep) | |
BEGIN | |
declare @pkID int; | |
declare page_cursor CURSOR FOR select pkID FROM tblWorkPage WHERE fkPageID= @fkPageID AND HasBeenPublished= @PublishedVersions AND fkLanguageBranchID = @languageID ORDER BY pkID ASC | |
open page_cursor | |
fetch next from page_cursor into @pkID | |
while @@FETCH_STATUS = 0 | |
BEGIN | |
EXEC editDeleteContentVersion @WorkContentID=@pkID | |
fetch next from page_cursor into @pkID | |
END | |
close page_cursor | |
deallocate page_cursor | |
SET @ObsoleteVersions= @ObsoleteVersions - 1 | |
END | |
fetch next from language_cursor into @languageID | |
END | |
close language_cursor | |
deallocate language_cursor | |
fetch next from db_cursor into @fkPageID | |
END | |
close db_cursor | |
deallocate db_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment