SQL SERVER – RETAINDAYS Does Not Delete Backup After x Days

RETAINDAYS sets backup expiration for overwrite checks, without deleting the file. I keep SQL expiration separate from Windows file cleanup.

A weathered sealed case remains on its shelf beside an empty transport tray.

A customer expected RETAINDAYS to delete older backup files. The files remained after the configured interval. That recurring question prompted the distinction between expiration and cleanup.

-- Backup template: SQL Server service account needs access to this destination.
-- BACKUP DATABASE [YourDatabase]
-- TO DISK = N'D:\SQLBackups\YourDatabase_full.bak'
-- WITH RETAINDAYS = 7;

RETAINDAYS sets an expiration interval used by SQL Server’s overwrite checks. Afterward, an appropriate backup operation can overwrite the backup set. The option doesn’t schedule deletion.

INIT attempts to replace existing backup sets while retaining the media header, subject to the normal checks. SKIP bypasses expiration and name checks, while FORMAT creates a new media set and can overwrite existing media. The default NOINIT appends rather than replacing existing backup sets.

A Windows file operation can still delete a .bak file regardless of this SQL Server expiration value. RETAINDAYS is therefore not immutable retention protection. Design a separate cleanup policy that respects full, differential and log restore dependencies, then preview the exact files before deletion.

Reference: Backup expiration and overwrite options.

Backup expiration is not scheduled deletion, it is an input to SQL Server overwrite checks.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Backup and Restore, SQL Scripts, SQL Server
Previous Post
SQL SERVER – 5 Don’ts When Database Corruption is Detected
Next Post
SQL SERVER – Msg 1038 – An Object or Column Name is Missing or Empty. For SELECT INTO Statements, Verify Each Column Has a Name

Related Posts

2 Comments. Leave new

  • Hallo Dave,
    I remember that there was an option for limit number of last database backups in SQL 2008 (R2) but I cannot find it in the SQL 2016 any more.
    Similarly there was some “graphical” interface to define maintenance jobs and it has also gone…

    Reply
  • If you could point all those mislead people to how properly make backups get deleted after X days… w/o that info this post is pointless.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.