SQL Server – How to Delete Old Differential Backup Files

backupsql server

I'm (trying) managing a large number of sql server instances among a number of machinces running SQL Server 2005 and 2008. Almost all the DB are in Simple Recovery Model.
I'm doing full backups every Sunday and differencial backups every day.
Even though I am using compressed backups, there are databases which are nearly a TB. Therefore, I need to have a proper use of the backups regarding the space they are allocating.

My goal: I just need to have one full and one differencial backup for each database every single moment.

My question : What actions should I perform in order to achieve my goal? I guess something i need to do with the expiration of the backups and the "Clean up history", but I am not sure what exactly i need to do.

Best Answer

Method1: The Best method to achieve this would be using Olla hallengren Backup scripts.

You can find the scripts at https://ola.hallengren.com/downloads.html

First thing you need to do is execute CommandExecute.sql script and then run the databasebackup.sql script.

Once done you can execute below via SQL Agent job as per you're required schedule:

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

Here Backup type can be changed from FULL, DIFF to LOG(If any DB in full recovery model exist) as per the need. And fro @ cleanup time- Specify the time, in hours, after which the backup files are deleted. If no time is specified, then no backup files are deleted.

Refer to below link on further details:

https://ola.hallengren.com/sql-server-backup.html

Method 2: You can always refer to Maintenance plans for clean up within SQL server

Use below query as an example to delete the old backup files as per the need:.

 EXECUTE MASTER.dbo.xp_delete_file 0,N'E:\Program Files\Microsoft SQL Server\MSSQL12.SQL2014\MSSQL\Backup',N'bak',N'2014-09-30T10:14:59'
GO

Refer to below link for using setting up cleanup task.

http://www.biztalkadminsblogging.com/index.php/blog-archive/item/20-automatically-remove-old-backup-files