Skip to content

Log cannot cleanup as expect #932

Open
@ChuckPerry

Description

@ChuckPerry

Description of the issue

Hi ,
When I set @Cleanuptime=2 when execute log backup every 10 min, found that log won't be auto cleanup if the time diff older that 2 hours. Check the CommandLog table, all the time shows same.

DECLARE @ReturnCode int EXECUTE @ReturnCode = dbo.xp_delete_file 0, N'E:\mssqlbackups\ai_studio\LOG', 'trn', '2025-05-29T00:00:00' IF @ReturnCode <> 0 RAISERROR('Error deleting files.', 16, 1)

Checked in the script, when execute the command , @CurrentLatestBackup will always be set to the same value. Please help fix or add comments if any misunderstand.

IF @CurrentBackupType = 'LOG' AND (@CleanupTime IS NOT NULL OR @MirrorCleanupTime IS NOT NULL)
      BEGIN
        SELECT @CurrentLatestBackup = MAX(backup_start_date)
        FROM msdb.dbo.backupset
        WHERE ([type] IN('D','I')
        OR ([type] = 'L' AND last_lsn < @CurrentDifferentialBaseLSN))
        AND is_damaged = 0
        AND [database_name] = @CurrentDatabaseName
      END

SQL Server version and edition

Microsoft SQL Server 2019 (RTM-CU25) (KB5033688) - 15.0.4355.3 (X64)   Jan 30 2024 17:02:22   Copyright (C) 2019 Microsoft Corporation  Standard Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor) 

Version of the script

==> Version: 2025-05-24 16:07:41

What command are you executing?

EXECUTE [dbo].[DatabaseBackup]
  @Databases = 'USER_DATABASES',
  @Directory = 'E:\mssqlbackups',
  @BackupType = 'LOG',
  @ChangeBackupType = 'Y',
  @Verify = 'Y',
  @CleanupTime = 2,
  @Checksum = 'Y',
  @LogToTable = 'Y',
  @LogSizeSinceLastLogBackup = 1024,
  @TimeSinceLastLogBackup = 600```

**What output are you getting?**

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions