USE [DocumedisUsageLogs]
GO

/*
MDDOC-878

Purge of the table DocumedisUsageLogs.dbo.Documedis2020UsageLogs

Records that are more than 7 months old will be purged.

Deletion is made in batches to avoid locking.
*/
CREATE PROCEDURE [purge_Documedis2020UsageLogs]
AS
BEGIN
    DECLARE @cutoff DATE = DATEADD(MONTH, -7, CURRENT_TIMESTAMP);
    DECLARE @batch INT = 5000;
    DECLARE @row_count INT = 1;

    WHILE @row_count > 0
    BEGIN
        DELETE TOP(@batch) s
        FROM [dbo].[Documedis2020UsageLogs] s
        WHERE [s].[LogDateTime] > @cutoff;
        
        SET @row_count = @@rowcount;
    END
END
