How to delete a failed backup item in Recovery Services Vault programmatically (Powershell 7.2)

2024-12-12T14:29:32.7233333+00:00

I am trying, via Automation Account Runbook, to remove everything in a Recovery Service Vault to be able to delete it.
I have a Backup Item in failed state, the storage account with the file share is already deleted.

User's image

I need, via Powershell, to be able to undelete it, like I would do it via Portal.
I downloaded the code to remove the Recovery Services Vault directly from portal

# Using Az.RecoveryServices module version 7.3.0

$vault = Get-AzRecoveryServicesVault

# Disable soft delete
Set-AzRecoveryServicesVaultProperty -VaultId $vault.Id -SoftDeleteFeatureState "Disable"
Write-Output "Soft delete disabled for the vault"

# Disable Security features (Enhanced Security) to remove MARS/MAB/DPM servers.
Set-AzRecoveryServicesVaultProperty -VaultId $vault.id -DisableHybridBackupSecurityFeature $true
Write-Output "Disabled Security features for the vault"

$backupItemsAFS = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureStorage -WorkloadType AzureFiles -VaultId $vault.Id
foreach ($item in $backupItemsAFS)
{
  Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.Id -Force
  # If I try this get the error ->
  # Undo-deletion is only supported for AzureVMs. This method is not supported for other workloads.

  Disable-AzRecoveryServicesBackupProtection -Item $item -VaultId $vault.Id -RemoveRecoveryPoints -Force 
  # This will raise the error ->
  # Container is not yet undeleted. Please undelete the container that contains this datasource   before attempting to undelete the datasource.
}  

Am I missing something ?
Thanks for helping ;-)

Azure Backup
Azure Backup
An Azure backup service that provides built-in management at scale.
1,320 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,249 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,707 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.