SQL SERVER – PowerShell Script – Delete Old Backup Files in SQL Express

If you have worked with various SQL Server editions, then you would know that SQL Server Express edition does not have SQL Server Agent service. Due to this, we lose capabilities to automate a lot of things. Let us learn in this blog post how to delete old backup files in SQL Express.

SQL SERVER - PowerShell Script - Delete Old Backup Files in SQL Express deletebackup

This article is not specific to SQL Express but can be implemented in any edition of SQL Server. Since we can use maintenance plan in other SQL editions, it may not be very useful there. One of the main things DBAs want to automate is taking backups on a regular basis. I have gone through this world of internet and there are a million ways to automate SQL backups and the most basic, easy and popular one adopted is using a Task Scheduler method. Microsoft has a Knowledge Base Article to achieve the same.

Solarwinds

How to schedule and automate backups of SQL Server databases in SQL Server Express

While doing consulting with a client, I found that the server was having E drive almost full. Later I learned that they have adopted the above approach. Now, they asked me if there is an automated way to delete backup files which are older than ‘X’ number of days. This is because their disk space was getting filled quickly.

SOLUTION/WORKAROUND

Below is the script which can be used to achieve deletion of the files. We need to provide two parameters, Path and DaysToKeep. The below script will hold the latest 5 days backup and delete all the older files from the folder provided in Path variable.

$Path = "D:\Backups\"
$DaysToKeep = "-5"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($DaysToKeep)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse

Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse

Note: this would delete any file in that location so if you want, you can add a filter to delete only .bak or .trn file.

USAGE STEPS

  1. Save the above script to a folder. for example: c:\temp\cleanupjob.ps1
  2. Open the Windows Task Scheduler. In the Task Scheduler, select the Create Task option
  3. Enter a name for the task, and give it a description (the description is optional)
  4. Click on the Triggers tab and set your schedule or event that will trigger the running of your PowerShell script
  5. Next, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.
  6. In the Program/script box enter “exe
  7. In the Add arguments box enter the value ->

-ExecutionPolicy Bypass c:\temp\cleanupjob.ps1

  1. Save your scheduled task and run it.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
, ,
Previous Post
SQL SERVER – FIX: Backup Detected Log Corruption in database MyDB. Context is Bad Middle Sector
Next Post
SQL SERVER – AlwaysOn Automatic Seeding Failure – Failure_code 15 and Failure_message: VDI Client Failed

Related Posts

Leave a Reply

Menu