SQL Server – Custom Backup Deletion with Ola Scripts

ola-hallengrensql server

I am trying to find a way to delete backups being taken by Ola's native SQL backups. I am trying to keep only 14 days of transaction logs, 1 month of diffs, and 12 months of full weekend backups. Is there a way to do it in Ola's scripts?

Best Answer

You can use the CleanupTime parameter for each backup job (log, diff, full)

Specify the time, in hours, after which the backup files are deleted. If no time is specified, then no backup files are deleted.

So for example:

@Cleanuptime = 336  --2 weeks for transaction logs 
@Cleanuptime = 744  --1 month for diff backups 
@Cleanuptime = 8760 --1 year for full backups

Example A from Ola

A. Back up all user databases, using checksums and compression; verify the backup; and delete old backup files

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@Verify = 'Y',
@Compress = 'Y',
@CheckSum = 'Y',
@CleanupTime = 24