/AzSubscriptionCleaner

Delete automatically the useless resources in your Azure subscription.

Primary LanguagePowerShellGNU General Public License v3.0GPL-3.0

Az Subscription Cleaner

Deploy to Azure GLO Board Serverless Library

Azure Subscription Cleaner

The Simple way to keep your Azure Subscription "clean". This run on a schedule and automatically delete all "expired" resources inside your Azure Subscription, and nothing else.

A Resource is "expired" when it has a tag expireOn older then the current date.


How to deploy

There is two version of this tool. One leveraging the Azure Function the second using Azure Automation. Both are valid options it just depends on your preferences.

Created using the PowerShell Azure Function all details is available in the azure-function folder

The Azure Automation Runbook will execute a PowerShell Script base on a pre-define schedule.

All details are available in the azure-automation folder

To deploy all the resources in your Azure subscription, just click on the Deploy to Azure button.


How Does it works

By Default, the tool will get triggered every morning at 5am and search for any resources tagged with expireOn with a value in the past, and delete them.

Once all the "expired" resources are deleted. It will search for empty Resource Group and delete them too.


Manage Tags

To be able to delete some resources you need to tag them with a tag expireOn and a date as value following the format YYYY-MM-dd.

From the Azure Portal

To add a tag from the portal select any resource. Then from the left panel select the ** Tags** option and add a tag with the name expireOn and the desired date.

add-tag-portal

With PowerShell

The following PowerShell command will add an expireOn tag with the value "2019-08-29" to the resource named demoWebsite in the resource group summerDemo.

    Set-AzResource -ResourceId (Get-AzResource -ResourceGroupName summerDemo -Name demoWebsite).ResourceId -Tag @{expireOn="2019-08-29"}

You could also add tags to a resource group and all its resources. This script will add the tag expireOn with the value "2019-08-29" add populate all resources to this resource group with the same tags.

Set-AzResourceGroup -ResourceId (Get-AzResourceGroup -Name "StreamCleaner").ResourceId -Tag @{expireOn="2019-08-29"}

$group = Get-AzResourceGroup "StreamCleaner"
if ($null -ne $group.Tags) {
    $resources = Get-AzResource -ResourceGroupName $group.ResourceGroupName
    foreach ($r in $resources) {
        $resourcetags = (Get-AzResource -ResourceId $r.ResourceId).Tags
        if ($resourcetags) {
            foreach ($key in $group.Tags.Keys) {
                if (-not($resourcetags.ContainsKey($key))) {
                    $resourcetags.Add($key, $group.Tags.$key)
                }
            }
            Set-AzResource -Tag $resourcetags -ResourceId $r.ResourceId -Force
        }
        else
        {
            Set-AzResource -Tag $group.Tags -ResourceId $r.ResourceId -Force
        }
    }
}

With Azure CLI

It's also possible using Azure CLI.

To add a tag `expireOn' with a value "2019-08-29" to the website demoWebsite IF the resource doesn't have existing tags, use:

az resource tag --tags expireOn=2019-08-29 -g summerDemo -n demoWebsite --resource-type "Microsoft.Web/sites"

This will delete any existing tags on that resource. If the resource has already tags use instead the following code.

To add a tag `expireOn' with a value "2019-08-29" to a website demoWebsite that already has tags, retrieve the existing tags, reformat that value, and reapply the existing and new tags:

jsonrtag=$(az resource show -g summerDemo -n demoWebsite --resource-type "Microsoft.Web/sites" --query tags)

rt=$(echo $jsonrtag | tr -d '"{},\n' | sed 's/: /=/g')

az resource tag --tags $rt expireOn=2019-08-29 -g summerDemo -n demoWebsite --resource-type "Microsoft.Web/sites"

To delete a specific tag value e.g. tag expireOn=2019-08-29 on all Resources, use:

for n in $(az tag list [--subscription <Subscription ID>] | fgrep -w *expireOn* | fgrep -w *2019-08-29*)
do
az tag remove-value --name expireOn --value 2019-08-29
done

To Learn more how to manage tags using PowerShell and Azure CLI visit bit.ly/azureTags

Azure Subscription

If you don't own an Azure subscription already, you can create your free account today. It comes with 200$ credit, so you can experience almost everything without spending a dime.

Create your free Azure account today


Current Status, Future Features

There is a Glo board to see what's in progress and on the road map. Feel free to great issues to request new feature or if you find bugs.


Contributing

Want to contribute? Check out our Code of Conduct and Contributing docs. This project follows the all-contributors specification. Contributions of any kind welcome!

Thanks goes to these wonderful people (emoji key):


Frank Boucher

💻 📖

Christopher Sl.

📖 💻

Alex Khil

💻