Always On

Remove-sqmDatabaseFromAG

Removes one or more databases from their Always On Availability Group and drops them from all secondary replicas. Auto-detects the AG the database belongs to. Supports removing a single database, a list, or all AG databases at once.

Module: sqmSQLTool
Requires: dbatools
ShouldProcess: Yes
Output: PSCustomObject[]

Execution Flow

START dbatools available? throw error Get-DbaAvailabilityGroup — enumerate all AGs on instance -All switch? Yes Get-DbaAgDatabase → all AG members No → use -Database list foreach target database Auto-detect AG membership (Get-DbaAgDatabase) Skip if DB not in any AG → Status=NotInAG ShouldProcess: Remove-DbaAgDatabase (from primary) foreach secondary replica ShouldProcess: Remove-DbaDatabase (drop on secondary) Return PSCustomObject[] (one result per action) DONE
Each database produces two result entries: one for removal from the AG (primary), and one per secondary replica where the database is dropped. Status codes: RemovedFromAG, DroppedOnSecondary, NotInAG, RemoveFromAGFailed, DropOnSecondaryFailed, RemoveSkipped (WhatIf), DropSkipped (WhatIf).

Parameters

ParameterTypeRequiredDescription
-SqlInstanceStringOptionalPrimary replica SQL instance. Default: $env:COMPUTERNAME.
-SqlCredentialPSCredentialOptionalSQL authentication credential.
-DatabaseString[]Optional*One or more database names to remove. Ignored when -All is set.
-AllSwitchOptional*Removes all user databases that are currently members of any AG.
-EnableExceptionSwitchOptionalThrows terminating errors instead of returning an error status object.
-WhatIf / -ConfirmSwitchOptionalStandard ShouldProcess support.

* Either -Database or -All must be provided.

Return Value

Returns an array of PSCustomObject, one per action, each with: SqlInstance, DatabaseName, Status, Message.

Examples

Example 1 — Remove a single database from its AG

Remove-sqmDatabaseFromAG -SqlInstance "SQL01" -Database "SalesDB"

Example 2 — Preview removal of all AG databases

Remove-sqmDatabaseFromAG -SqlInstance "SQL01" -All -WhatIf