Categories
Sql Server

How To Delete Backup Files Older Than n Days

How To Delete Backup Files Older Than n Days

In this post we will learn How To Delete Backup Files Older Than n Days. This will help us in deleting old file to free space from disk, we can do it manually. But by this approach we can delete files by setting number of days to keep and delete others. Earlier we had discussed Creating Resource Pools in SQL ServerResource Governor in SQL ServerSwitch Back to iOS after JailbreakHow to Jailbreak iOS 7.1.2, iOS 7.1.1 and iOS 7.1.

We are doing this with the help of XP_CMDSHELL feature in SQL Server.

To Enable XP_CMDSHELL in SQL Server:

Use Master
GO

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO

EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO

XP_CMDSHELL Script:

----Here -30 stand for Delete Files older than 30 Days From Today----
----You Can change this value according to you----
----c:\BACKUP is the path where your files are-----
----*.bak is for delete files who have .bak extension----

EXEC xp_cmdshell 'FORFILES /p c:\BACKUP /s /m *.bak /d -30 /c "CMD /C del /Q /F @FILE"'