Last Updated on August 21, 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";
}
FAQ:
Q: How can I use Azure Automation to automatically delete unused resources?
To automatically delete unused resources, you can create a runbook in Azure Automation that runs a PowerShell script. This script can automatically delete resources based on specific criteria, such as being unused or unnecessary. This helps in keeping your Azure environment clean by ensuring that resources are managed more efficiently.
Q: What is the best way to clean up resources in Azure using PowerShell?
To clean up resources in Azure using PowerShell, you can create a script that identifies and deletes unused resources. This script can be executed manually or run on a schedule using Azure Automation. The script should target resource groups and resources that are unused or unnecessary to ensure a clean and optimized Azure environment.
Q: How can I delete an empty resource group in the Azure portal?
In the Azure portal, you can delete an empty resource group by navigating to the resource group in question and selecting the delete option. Before doing so, ensure that the resource group is indeed empty to avoid accidentally deleting any important resources.
Q: What tools can be used to automate the deletion of resources in Azure?
You can use several tools to automate the deletion of resources in Azure, such as Azure Automation, Azure CLI, and Azure Functions. These tools allow you to schedule and run scripts that identify and remove unused resources, keeping your environment clean and optimized.
Q: How do I ensure that I donโt accidentally delete important resources in Azure?
To avoid accidentally deleting important resources, you can use tags and Azure Policy to mark critical resources. Additionally, always review the resources listed for deletion in your scripts or Azure portal before confirming the deletion. It’s also wise to add a tag to check in your automation scripts to ensure that resources still in use are not deleted.
Q: What is the role of a runbook in managing Azure resources?
A runbook in Azure Automation allows you to automate repetitive tasks, such as resource cleanup. By creating a runbook, you can schedule it to run every morning or at any specified interval to ensure that unused resources are automatically identified and deleted, keeping your Azure subscription clean and cost-effective.
Q: How can Azure Policy help in managing resource cleanup?
Azure Policy can help in managing resource cleanup by enforcing rules and ensuring that resources are compliant with your organizational standards. For instance, you can create a policy that automatically identifies and flags unused or unnecessary resources for deletion, ensuring that resource cleanup in Azure is managed systematically.
Q: How can I use tags to manage and delete resources more efficiently in Azure?
You can use tags in Azure to categorize resources based on specific criteria, such as usage or expiration. When running automated scripts or Azure Automation runbooks, these tags can be referenced to determine which resources need to be deleted. This method helps you manage and delete resources more efficiently.
Q: What is the best way to delete all the resources inside a resource group except for certain resources?
To delete all resources inside a resource group except for specific ones, you can use a PowerShell script or Azure CLI command that loops through the resources in the group, applying a filter to exclude certain resources based on their tag name or other identifiers. This ensures that only the targeted resources are deleted.
Q: How can I prevent the accidental deletion of important resources in Azure Automation scripts?
To prevent accidental deletion, ensure that your Azure Automation scripts have checks in place, such as verifying tag names, confirming resource status, or requiring additional manual approval before deletion. This helps to avoid scenarios where critical resources are mistakenly removed.
Q: How can I delete the resource using Azure CLI?
A: You can use the az resource
command in Azure CLI to delete a resource. Ensure you have the necessary permissions to delete the resource.
Q: What should I do if I may accidentally delete a resource?
A: It’s important to manage your Azure resources carefully and ensure that resources are tagged appropriately to avoid accidental deletion. Use Azure Resource Manager and configure it properly to prevent mishaps.
Q: How do I run a script every morning using Azure Automation?
A: You can create a runbook in your Azure Automation account and configure it to run every morning. This is particularly useful for automating resource cleanup or other routine tasks.
Q: What is the best way to automate resource deletion with PowerShell?
A: You can use Azure PowerShell to automate the deletion of resources. Write a PowerShell script to identify and delete specific resources, and you can even schedule this task using an Azure Automation runbook.
Q: How do I navigate repository files on GitHub?
A: You can navigate repository files on GitHub by using the repository files navigation feature, which allows you to browse through folders and files in a repository easily.
Q: What is the importance of resource groups in Azure?
A: Resource groups in Azure are essential for managing your Azure resources efficiently. They allow you to organize and manage resources like virtual machines, storage accounts, and more within a single container.
Q: How can I avoid forgetting to delete unused resources?
A: Automating resource cleanup is a good practice to avoid forgetting to delete unused resources. You can set up scripts or runbooks to manage this automatically.
Q: How do I delete a resource group in Azure?
A: You can delete a resource group in Azure using Azure CLI, PowerShell, or the Azure portal. This will also delete all resources within that resource group, so proceed with caution.
Q: What is the significance of tagging resources in Azure?
A: Tagging resources in Azure is important for organizing and managing your resources effectively. Tags can be used to identify resources by department, environment, or any other criteria, making it easier to manage large environments.
Q: What should I do if I forget to delete a resource group?
A: If you forget to delete a resource group, you can manually delete it later using Azure CLI, PowerShell, or the Azure portal. It’s also a good idea to automate this process to avoid leaving unused resources running.
Q: How can I get additional resources and learning materials for managing Azure resources?
A: Microsoft Learn provides a wealth of additional resources and learning materials for managing Azure resources, including tutorials, documentation, and best practices.
Q: How do I create a new runbook in Azure Automation?
A: To create a new runbook in Azure Automation, open a new runbook in your Azure Automation account, enter a name, and start scripting your automation tasks.
Q: What should I do before deleting a resource or resource group?
A: Before deleting a resource or resource group, ensure that you have reviewed all the resources within it and understand the dependencies. This helps in avoiding unintended service disruptions.