Script PowerShell : Delete VM Item backup and the Recovery Service Vault

Brass Contributor

***Steps :

-Disable soft delete for the Azure Backup Recovery Services vault

-Check if there are backup items in a soft-deleted state and reverse the delete operation

-Stop protection and delete data for all backup-protected items

-Delete the Recovery Services vault

 

*******************************************************************************************

## Variables

 

$rgBackup = "RG_Name" 

$rgBackupInstanRecovery = "RG_Name" 

$vaultName = "vault_name" 

$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ## Disable soft delete for the Azure Backup Recovery Services vault

 

Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable

 

Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`

 

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Check if there are backup items in a soft-deleted state and reverse the delete operation

 

$containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureWorkload -WorkloadType MSSQL  -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}

 

foreach ($item in $containerSoftDelete) {

    Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose

}

 

Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Stop protection and delete data for all backup-protected items

 

$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}

 

foreach ($item in $containerBackup) {

    Disable-AzRecoveryServicesBackupProtection -Item $item -VaultId $vault.ID -RemoveRecoveryPoints -Force -Verbose

}

 

Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)`

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Delete the Recovery Services vault

 

Remove-AzRecoveryServicesVault -Vault $vault -Verbose

 

Write-Host ($writeEmptyLine + "# Recovery Services vault " + $vault.Name + " deleted" + $writeSeperatorSpaces + $currentTime)

0 Replies