Auto Clean Azure Resources – Using Azure Automation Auto Clean Azure Resources – Using Azure Automation

Last Updated on February 17, 2024 by Arnav Sharma

Here’s an easy way to clean up Azure Subscription and delete all resource groups, plus resources to save some credits.

Step 1 is to create a resource group named ‘automation’ (or change the name in the script below) and create an automation account. Ensure that the “System Assigned” identity is checked while account creation.

Step 2, go to Subscription and enable contributor access for the automation account.

Step 3, Go to the Automation account and click on runbook and create a new account.

Step 4, paste the PowerShell script here and hit Save, followed by Publish

Step 5, click on start to test the script or in case you want to schedule it, click on Schedules inside the runbook and add a schedule as per your requirement.

The automation account has contributor access on the Sub., so when the runbook will trigger at a specific time, it will clean off all resource groups and resources.

Script:


# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process

# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext

Write-Output "Using system-assigned managed identity"

#Get Azure Resource Groups
$allresourcegroups = Get-AzResourceGroup | Where-Object ResourceGroupName -NotLike '*automation*' ##exception is set here
  
     
    if(!$allresourcegroups){
        Write-Output "No resource groups found";
    }
    else{
         
        Write-Output "Starting the cleanup process";
           
            foreach($resourceGroup in $allresourcegroups){

                $rgname = $resourceGroup.ResourceGroupName

                    Write-Host "Deleting $($resource.ResourceGroupName)..."
                    Remove-AzResourceGroup -Name $rgname -Force
        }
         
        Write-Output "Cleanup Completed";
         
    }


Q: What is Azure?

A: Azure is a cloud computing platform developed by Microsoft that provides a range of cloud services for building, deploying, and managing applications and services through a global network of Microsoft-managed datacenters.

Q: What are Azure resources?

A: Azure resources are the various services and components that can be created and managed within an Azure subscription, such as virtual machines, storage accounts, web apps, and databases.

Q: What is Azure Automation?

A: Azure Automation is a cloud-based automation and configuration management service by Microsoft that provides tools to automate the creation, deployment, monitoring, and maintenance of resources in Azure.

Q: Can I use PowerShell to automate Azure resource cleanup?

A: Yes, you can use PowerShell scripts to automate resource cleanup in Azure. You can use Azure PowerShell cmdlets or the Azure CLI to identify and delete unused or unnecessary resources in your subscription.

Q: How can I use Azure Automation to automatically clean up resources?

A: You can use Azure Automation to create a runbook, which is a PowerShell script that automates a specific task, such as resource cleanup. You can schedule the runbook to run every morning, for example, and configure it to automatically delete resources that have not been used for a specified time period.

Q: Can I use Azure Portal to clean up resources?

A: Yes, you can use the Azure Portal to identify and manually delete unused or unnecessary resources in your subscription. You can also use the query functionality to search for resources that are not being used, such as those with expired or soon-to-expire expiration dates.

Q: What happens if I forget to delete unnecessary resources?

A: If you forget to delete unnecessary resources, they will continue to be billed, which can result in higher costs for your subscription over time. Automating resource cleanup can help keep your Azure subscription lean and cost-effective.

Q: How can I automate resource cleanup using Azure Function?

A: You can use Azure Function to trigger a PowerShell script that checks for unused resources and deletes them. You can use the PowerShell script to query the Azure Resource Manager API to identify resources that need to be deleted.

Q: Can I automatically delete empty resource groups?

A: Yes, you can use Azure Automation or an Azure Function to check for empty resource groups and automatically delete them. You can use the Azure Resource Manager API to query for empty resource groups and delete them if they contain no resources or only expired or unnecessary resources.

Q: How can I identify and tag resources in Azure?

A: You can use Azure CLI or Azure PowerShell cmdlets to tag resources in Azure. You can tag resources with the name, project, department, or other relevant information. Tagging resources can help you identify and manage resources more efficiently.


keywords: execute, deletion, deletion, az, environment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Toggle Dark Mode