Delete Files Older Than N Days
SQL Server 2000: Tested
SQL Server 2005: Tested
SQL Server 2008: Tested
SQL Server 2008R2: Tested
SQL Server 2012: Not Tested
Date: 1 Dec 2011
With the increasing need for security and the more secure default configurations delivered by Microsoft, the use of xp_cmdshell has not only become less desirable but also requires changing system security in a way that often contravenes IT Department guidelines.
For this reason, I eventually relented and created this small batch file to remove old files.
This now forms a core part of smaller VM SQL Server installations, where I use a combination of local SQL Native and VM-level backups, thus requiring a file cleanup of the SQL backup files.
I'm far from a batch file guru, so the functionality is limited, and the process will fail if there are no files to delete. Conversely, this can be a supplemental way of discovering that backups are not working.
Echo Deleting files older than 4 days in the specified directory !
forfiles /p %CurDir% /S /D -4 /c "cmd /c del /Q /F /S "@file""
Echo You have successfully deleted the specified files in your directory and all subdirectories !