join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



Free SQL Server monitoring resources

Maintenance task to delete old SQL Server backup files

Written By: Greg Robidoux -- 9/6/2007 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
In two previous tips we discussed how to automate full backups and transaction log backups by creating scripts that iterate through each of your databases and then execute the backup commands.  A reader requested information about how to automate the process of deleting older backup files, so this tip explains one approach for getting rid of older backup files that are generated.

Solution
In previous tip we took a look at using Windows Scripting (Simple way to find errors in SQL Server error log) to read through the error log files and generate a slimmer error file with just the error messages and the related messages that were created at the same time.  In this tip, we will also be using Windows Scripting to go through each of the subfolders to find files older than a certain timeframe and then delete these files.

Here is the VBScript code.  This was pulled together from a few different code snippets found on the internet. 

There are two parameters that need to be adjusted:

  • iDaysOld - specify how many days old the files need to be for the script to delete them
  • strPath - this is the folder where the backups are created.
iDaysOld 7
strPath 
"C:\BACKUP"

Set objFSO CreateObject("Scripting.FileSystemObject"
Set objFolder objFSO.GetFolder(strPath
Set colSubfolders objFolder.Subfolders 
Set colFiles objFolder.Files 

For Each objFile in colFiles 
   
If objFile.DateLastModified < (Date() - iDaysOldThen 
       
MsgBox "Dir: " objFolder.Name vbCrLf "File: " objFile.Name
       
'objFile.Delete 
   
End If 
Next 


For Each 
objSubfolder in colSubfolders 
   
Set colFiles objSubfolder.Files 
   
For Each objFile in colFiles 
       
If objFile.DateLastModified < (Date() - iDaysOldThen 
           
MsgBox "Dir: " objSubfolder.Name vbCrLf "File: " objFile.Name
           
'objFile.Delete 
       
End If 
   Next 
Next 

Setup

  • To use this script first create a new text file and copy and paste the above code.  Save this new file as C:\DeleteBackupFiles.vbs or whatever you would like to call it.
  • Create another new text file and copy and paste the code below. .(If you rename the file or place it in another folder use this instead of the info below.)  Save this new file as C:\DeleteBackupFiles.bat.
C:\DeleteBackupFiles.vbs

Note: As a safeguard the script just displays a message box with the folder and file name.  To actually delete the files you will need to remove the single quote ' before the two delete lines.:

  • 'objFile.Delete

At this point you can just run the BAT file and this will delete any files in the subfolders. 

The way this works is it will delete any files that it finds in the subfolders after the starting point.  It does not care about what kind of files they are, it will delete all files that are older than the timeframe specified.  The script also will delete files in the root folder and any files one subfolder level deep. It does not go beyond the first subfolder level.

So if you specify your backups to be in "C:\Backup" this script will delete all files in the "C:\Backup" folder as well as any files in the first level subfolders as long as the date of the file is older than specified.

This can now be setup as a scheduled job by calling the BAT file.  SQL Server Agent doesn't like running VBScript files directly, so by using a BAT file you can set this up as a scheduled job.

Next Steps

  • Use this script as is or modify this to have additional options that meet your needs such as specifying certain types of files.
  • The script uses the DateLastModified property, but this could also be changed to the DateCreated property.
  • Modify the script to add some logging, so you can see what has been deleted.
  • The maintenance plans use XP_DELETE_FILE (2005) and SQLMaint (2000) to delete the older files.  You could look at these options as well.
  • Take a look at the other backup scripts to automate your entire backup process.
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Free trial: Red Gate SQL Response for no-nonsense monitoring & alerting of SQL Server health & activity. Download now.

Make the most out of SQL Server - Guaranteed Results - Innovative SQL Server DBAs

Follow MSSQLTips on Twitter!

Free white paper - Simplify SQL Server Management: Helpful SQL Server Tips


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Idera - SQL safe backup

Need more space for your backups? Idera has announced the latest edition of SQL safe, their award-winning backup and recovery solution. SQL safe v6.0 includes brand new compression algorithms developed with some of the leading compression experts in the world. Plus, SQL safe 6.0 continuously optimizes compression to give you the fastest possible backup with the highest possible compression, every time you do a backup.

Download now!

More SQL Server Tools
SQL Backup

SQL safe backup

SQL defrag manager

SQL Prompt

SQL diagnostic manager




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com