Delete Resource Group If Exists (AZ Module)

Octopus.AzurePowerShell exported 2020-04-13 by octobob belongs to ‘Azure’ category.

This step uses the new az modules to delete a resource group if it exist.

Requires Octopus Deploy 2020.1 or later.

Requires a worker with the az module installed on it. That module is not bundled with Octopus Deploy.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Azure Account

DeleteResourceGroup.Azure.Account =

The Azure Account with the necessary permissions to create resource groups.

Resource Group Name

DeleteResourceGroup.ResourceGroup.Name =

The name of the resource group to create.

The Resource Group Abbreviated Location

DeleteResourceGroup.ResourceGroup.Location.Abbr =

The abbreviated resource group location, for example: centralus

Script body

Steps based on this template will execute the following PowerShell script.

$resourceGroupName = $OctopusParameters["DeleteResourceGroup.ResourceGroup.Name"]
$resourceGroupLocationAbbr = $OctopusParameters["DeleteResourceGroup.ResourceGroup.Location.Abbr"]

$existingResourceGroups = (az group list --query "[?location=='$resourceGroupLocationAbbr']") | ConvertFrom-JSON

$deleteResourceGroup = $false
foreach ($resourceGroupFound in $existingResourceGroups)
{	
	Write-Host "Checking if current resource group $($resourceGroupFound.name) matches $resourceGroupName"
    if ($resourceGroupFound.name -eq $resourceGroupName)
    {
    	$deleteResourceGroup = $true
    	Write-Highlight "Resource group found, deleting"
    	break
    }
}

if ($deleteResourceGroup)
{
	Write-Host "Deleting the $resourceGroupName because it was found"
	az group delete -n $resourceGroupName -y
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "3d61fa51-3352-4af6-b532-7ae1f48218b7",
  "Name": "Delete Resource Group If Exists (AZ Module)",
  "Description": "This step uses the new `az` modules to delete a resource group if it exist.  \n\nRequires `Octopus Deploy 2020.1` or later.\n\nRequires a worker with the `az` module installed on it.  That module is not bundled with Octopus Deploy.",
  "Version": 1,
  "ExportedAt": "2020-04-13T15:37:29.866Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "octobob",
  "Packages": [],
  "Parameters": [
    {
      "Id": "b854aba4-4acb-428d-b981-79d1f2f50537",
      "Name": "DeleteResourceGroup.Azure.Account",
      "Label": "Azure Account",
      "HelpText": "The Azure Account with the necessary permissions to create resource groups.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AzureAccount"
      }
    },
    {
      "Id": "c7e43d70-f9ac-43cb-95a8-6a0e75ad40c6",
      "Name": "DeleteResourceGroup.ResourceGroup.Name",
      "Label": "Resource Group Name",
      "HelpText": "The name of the resource group to create.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e1bd7e57-5ad0-445b-9d11-6642acc55b88",
      "Name": "DeleteResourceGroup.ResourceGroup.Location.Abbr",
      "Label": "The Resource Group Abbreviated Location",
      "HelpText": "The abbreviated resource group location, for example: centralus",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "centralus|Americas - Central US\neastus|Americas - East US\neastus2|Americas - East US 2\nnorthcentralus|Americas - North Central US\nsouthcentralus|Americas - South Central US\nwestus|Americas - West US\nwestus2|Americas - West US 2\nwestcentralus|Americas - West Central US\ncanadacentral|Americas - Canada Central\ncanadaeast|Americas - Canada East\nbrazilsouth|Americas - Brazil South\neastasia|Asia Pacific - East Asia\nsoutheastasia|Asia Pacific - Southeast Asia\naustraliacentral|Asia Pacific - Australia Central\naustraliacentral2|Asia Pacific - Australia Central 2\naustraliaeast|Asia Pacific - Australia East\naustraliasoutheast|Asia Pacific - Australia Southeast\nchinaeast|Asia Pacific - China East\nchinaeast2|Asia Pacific - China East 2\nchinanorth|Asia Pacific - China North\nchinanorth2|Asia Pacific - China North 2\ncentralindia|Asia Pacific - Central India\nsouthindia|Asia Pacific - South India\nwestindia|Asia Pacific - West India\njapaneast|Asia Pacific - Japan East\njapanwest|Asia Pacific - Japan West\nkoreacentral|Asia Pacific - Korea Central\nkoreasouth|Asia Pacific - Korea South\nnortheurope|Europe - North Europe\nwesteurope|Europe - West Europe\nfrancecentral|Europe - France Central\nfrancesouth|Europe - France South\ngermanynorth|Europe - Germany North\ngermanywestcentral|Europe - Germany West Central\nnorwayeast|Europe - Norway East\nnorwaywest|Europe - Norway West\nspaincentral|Europe - Spain Central\nswitzerlandnorth|Europe - Switzerland North\nswitzerlandwest|Europe - Switzerland West\nuksouth|Europe - UK South\nukwest|Europe - UK West\nsouthafricanorth|Middle East and Africa - South Africa North\nsouthafricawest|Middle East and Africa - South Africa West\nuaecentral|Middle East and Africa - UAE Central\nuaenorth|Middle East and Africa - UAE North"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "OctopusUseBundledTooling": "False",
    "Octopus.Action.Azure.AccountId": "#{DeleteResourceGroup.Azure.Account}",
    "Octopus.Action.Script.ScriptBody": "$resourceGroupName = $OctopusParameters[\"DeleteResourceGroup.ResourceGroup.Name\"]\n$resourceGroupLocationAbbr = $OctopusParameters[\"DeleteResourceGroup.ResourceGroup.Location.Abbr\"]\n\n$existingResourceGroups = (az group list --query \"[?location=='$resourceGroupLocationAbbr']\") | ConvertFrom-JSON\n\n$deleteResourceGroup = $false\nforeach ($resourceGroupFound in $existingResourceGroups)\n{\t\n\tWrite-Host \"Checking if current resource group $($resourceGroupFound.name) matches $resourceGroupName\"\n    if ($resourceGroupFound.name -eq $resourceGroupName)\n    {\n    \t$deleteResourceGroup = $true\n    \tWrite-Highlight \"Resource group found, deleting\"\n    \tbreak\n    }\n}\n\nif ($deleteResourceGroup)\n{\n\tWrite-Host \"Deleting the $resourceGroupName because it was found\"\n\taz group delete -n $resourceGroupName -y\n}"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/azure-delete-resource-group-az-module.json",
  "Website": "/step-templates/3d61fa51-3352-4af6-b532-7ae1f48218b7",
  "Logo": "iVBORw0KGgoAAA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