Thor Logo dbatools

Remove-DbaDbSnapshot

View Source
Simone Bizzotto (@niphold)
Windows, Linux, macOS

Synopsis

Drops database snapshots from SQL Server instances

Description

Removes database snapshots by executing DROP DATABASE statements against the target SQL Server instances. Database snapshots are point-in-time, read-only copies of databases that consume minimal space through copy-on-write technology. This function helps DBAs clean up obsolete snapshots that are no longer needed for reporting, testing, or recovery purposes. The Force parameter can terminate active connections to snapshots that might otherwise prevent the drop operation from succeeding.

Syntax

Remove-DbaDbSnapshot
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-ExcludeDatabase] <String[]>]
    [[-Snapshot] <String[]>]
    [[-InputObject] <Database[]>]
    [-AllSnapshots]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Remove-DbaDbSnapshot -SqlInstance sql2014 -Snapshot HR_snap_20161201, HR_snap_20161101

Removes database snapshots named HR_snap_20161201 and HR_snap_20161101

Example: 2
PS C:\> Remove-DbaDbSnapshot -SqlInstance sql2014 -Database HR, Accounting

Removes all database snapshots having HR and Accounting as base dbs

Example: 3
PS C:\> Get-DbaDbSnapshot -SqlInstance sql2014 -Database HR, Accounting | Remove-DbaDbSnapshot

Removes all database snapshots having HR and Accounting as base dbs

Example: 4
PS C:\> Remove-DbaDbSnapshot -SqlInstance sql2014 -Snapshot HR_snapshot, Accounting_snapshot

Removes HR_snapshot and Accounting_snapshot

Example: 5
PS C:\> Get-DbaDbSnapshot -SqlInstance sql2016 | Where-Object SnapshotOf -like '*dumpsterfire*' | Remove-DbaDbSnapshot

Removes all snapshots associated with databases that have dumpsterfire in the name

Example: 6
PS C:\> Get-DbaDbSnapshot -SqlInstance sql2016 | Out-GridView -PassThru | Remove-DbaDbSnapshot

Allows the selection of snapshots on sql2016 to remove

Example: 7
PS C:\> Remove-DbaDbSnapshot -SqlInstance sql2014 -AllSnapshots

Removes all database snapshots from sql2014

Example: 8
PS C:\> Remove-DbaDbSnapshot -SqlInstance sql2014 -AllSnapshots -Confirm

Removes all database snapshots from sql2014 and prompts for each database

Optional Parameters

-SqlInstance

The target SQL Server instance or instances

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Database

Specifies the base database(s) whose snapshots should be removed. Only snapshots created from these source databases will be dropped.
Use this when you need to clean up snapshots for specific databases while leaving other snapshots intact.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-ExcludeDatabase

Excludes snapshots from the specified base database(s) from removal. All other snapshots on the instance will be removed.
Use this when you want to remove most snapshots but preserve those from critical databases.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Snapshot

Specifies the exact snapshot name(s) to remove. Accepts multiple snapshot names for targeted removal operations.
Use this when you know the specific snapshot names you want to drop, such as outdated test or reporting snapshots.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-InputObject

Accepts database snapshot objects from Get-DbaDbSnapshot for pipeline operations. This allows filtering and processing snapshots before removal.
Use this for complex filtering scenarios where you first identify specific snapshots with Get-DbaDbSnapshot.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-AllSnapshots

Removes all database snapshots found on the target SQL Server instance(s). This affects every snapshot regardless of source database.
Use this for complete snapshot cleanup operations, typically during maintenance windows or server decommissioning.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-Force

Terminates active connections and running queries against snapshots to allow the drop operation to complete successfully.
Use this when snapshots have active sessions that would normally block the removal process.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf

Shows what would happen if the command were to run

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

Prompts for confirmation of every step.

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value