dbatools is a free PowerShell module with over 300 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


Provides all of the same functionality for removing SQL backups from disk as a standard maintenance plan would.

As an addition you have the ability to check the Archive bit on files before deletion. This will allow you to ensure
backups have been archived to your archive location before removal.

Also included is the ability to remove empty folders as part of this cleanup activity.





To remove ‘*.trn’ files from ‘C:\MSSQL\SQL Backup\’ and all subdirectories that are more than 48 hours.

Remove-DbaBackup -BackupFolder ‘C:\MSSQL\SQL Backup\’ -BackupFileExtension trn -RetentionPeriod 48h

To perform the same as example #1, but using the WhatIf parameter. The WhatIf parameter will allow the command show you what it will do, without actually doing it.

In the example, no trn files will be deleted. Instead, the command will output what it will do when it runs. This is a good preventative measure especially when you are first configuring the cmomand calls.

Remove-DbaBackup -BackupFolder ‘C:\MSSQL\SQL Backup\’ -BackupFileExtension trn -RetentionPeriod 48h -WhatIf

To remove ‘*.bak’ files from ‘C:\MSSQL\SQL Backup\’ and all subdirectories that are more than 7 days old and also ensure that the bak files have been archived using the archive bit before removing them.

Remove-DbaBackup -BackupFolder ‘C:\MSSQL\Backup\’ -BackupFileExtension bak -RetentionPeriod 7d -CheckArchiveBit

To remove ‘*.bak’ files from ‘C:\MSSQL\Backup\’ and all subdirectories that are more than 1 week old. It will also remove any backup folders that no longer contain backup files.

Remove-DbaBackup -BackupFolder ‘C:\MSSQL\Backup\’ -BackupFileExtension bak -RetentionPeriod 1w -RemoveEmptyBackupFolders


This command was created by Chris Sommer. You can find Chris on his blog and on Twitter.


From PowerShell, execute Get-Help Remove-DbaBackup -Detailed for more information on this function.

Source Code

Want to see the source code? View Remove-DbaBackup.ps1 on GitHub

Related commands