SQL Server – Use T-SQL to delete files older than X days

Sometimes there is a need to delete backup files via tSQL. Here is a script that can do the trick.

— -d represents number of days, – 2 represents file older than 2 days set the parameter as needed

exec xp_CMDShell ‘Forfiles -p “C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA” -s -m *.trc -d -2 -c “Cmd /C del @FILE”‘

 

enjoy!

Comments
  1. 5 years ago

Leave a Reply

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