commands

^

Remove-DbaBackup

Author Chris Sommer (@cjsommer), www.cjsommer.com
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Remove-DbaBackup on GitHub.
Want to see the Bill Of Health for this command? Check out Remove-DbaBackup.

Synopsis

Removes SQL Server backups from disk.

Description

Removes SQL Server backups from disk.

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.

Syntax

Remove-DbaBackup
    [-Path] <String>
    [-BackupFileExtension] <String>
    [-RetentionPeriod] <String>
    [-CheckArchiveBit]
    [-RemoveEmptyBackupFolder]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Remove-DbaBackup -Path 'C:\MSSQL\SQL Backup\' -BackupFileExtension trn -RetentionPeriod 48h

'*.trn' files in 'C:\MSSQL\SQL Backup' and all subdirectories that are more than 48 hours old will be removed.

Example: 2
PS C:\> Remove-DbaBackup -Path 'C:\MSSQL\SQL Backup\' -BackupFileExtension trn -RetentionPeriod 48h -WhatIf

Same as example #1, but doesn't actually remove any files. The function will instead show you what would be done.
This is useful when first experimenting with using the function.

Example: 3
PS C:\> Remove-DbaBackup -Path 'C:\MSSQL\Backup\' -BackupFileExtension bak -RetentionPeriod 7d -CheckArchiveBit

'*.bak' files in 'C:\MSSQL\Backup' and all subdirectories that are more than 7 days old will be removed, but only if the files have been backed up to another location as verified by checking the
Archive bit.

Example: 4
PS C:\> Remove-DbaBackup -Path 'C:\MSSQL\Backup\' -BackupFileExtension bak -RetentionPeriod 1w -RemoveEmptyBackupFolder

'*.bak' files in 'C:\MSSQL\Backup' and all subdirectories that are more than 1 week old will be removed. Any folders left empty will be removed as well.

Required Parameters

-Path

Specifies the name of the base level folder to search for backup files. Deletion of backup files will be recursive from this location.

Alias BackupFolder
Required True
Pipeline false
Default Value
-BackupFileExtension

Specifies the filename extension of the backup files you wish to remove (typically 'bak', 'trn' or 'log'). Do not include the period.

Alias
Required True
Pipeline false
Default Value
-RetentionPeriod

Specifies the retention period for backup files. Correct format is ##U.

is the retention value and must be an integer value

U signifies the units where the valid units are:
h = hours
d = days
w = weeks
m = months
Formatting Examples:
'48h' = 48 hours
'7d' = 7 days
'4w' = 4 weeks
'1m' = 1 month

Alias
Required True
Pipeline false
Default Value

Optional Parameters

-CheckArchiveBit

If this switch is enabled, the filesystem Archive bit is checked before deletion. If this bit is set (which translates to "it has not been backed up to another location yet", the file won't be
deleted.

Alias
Required False
Pipeline false
Default Value False
-RemoveEmptyBackupFolder

If this switch is enabled, empty folders will be removed after the cleanup process is complete.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.i

Alias cf
Required False
Pipeline false
Default Value