commands

^

Remove-DbaDbSnapshot

Author Simone Bizzotto (@niphold)
Availability Windows, Linux, macOS

 

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

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

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline false
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.

Alias
Required False
Pipeline true (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.

Alias
Required False
Pipeline false
Default Value False
-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.

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

Shows what would happen if the command were to run

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts for confirmation of every step.

Alias cf
Required False
Pipeline false
Default Value