Skip to content

Instantly share code, notes, and snippets.

@ignas-sakalauskas
Created April 17, 2017 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ignas-sakalauskas/c02887ead0155679490879f1e11bb4e6 to your computer and use it in GitHub Desktop.
Save ignas-sakalauskas/c02887ead0155679490879f1e11bb4e6 to your computer and use it in GitHub Desktop.
Clean and shrink SQL database
SET NOCOUNT ON;
DECLARE @rows int = 1;
DECLARE @now datetime = GETDATE();
WHILE @rows > 0
BEGIN
BEGIN TRANSACTION;
DELETE TOP (500) [ASPState].dbo.ASPStateTempSessions
WHERE Expires < @now
SET @rows = @@ROWCOUNT;
COMMIT TRANSACTION;
CHECKPOINT;
END
-- https://ignas.me/tech/clean-and-shrink-sql-database/
ALTER DATABASE ASPState SET RECOVERY SIMPLE
DBCC SHRINKFILE (ASPState_Log, 1)
-- https://ignas.me/tech/clean-and-shrink-sql-database/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment