Let's say I have a resource group MyAppRG. It has a storage account, a SQL server, a keyvault, and a VM. How would I delete everything except the storage account and the keyvault in the resource group programmatically?
I know that there is a way to delete everything in a resource group by deploying an empty arm template deployment. I have tried using a Get-AzResource on the resources and adding it to the resources parameter to the ARM template, but it is not working. Something like this:
$storageAccounts = Get-AzStorageAccount -ResourceGroupName "MyAppRG" | Get-AzResource
$emptyArmTemplate = @{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
contentVersion: "1.0.0.0",
"resources": @(storageAccounts),
}
New-AzResourceGroupDeployment -Name "test" -ResourceGroupName "MyAppRG" -TemplateObject $emptyArmTemplate -Force
But this does not work as the request is invalid.