how to create a timer trigger function to delete resource group in azure sdk python

Sushant Seven 0 Reputation points

how to create an azure function to delete resource group

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,002 questions
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
2,700 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Saurabh Sharma 23,251 Reputation points Microsoft Employee

    Hi Sushant Seven,

    Thanks for using Microsoft Q&A!!

    In order to delete a resource group using Azure SDK in python, you need to use the Azure Resource management client library (azure-mgmt-resource) of python.

    Here is the code snippet using the above client library to delete the resource group through a scheduled triggered function-

    import datetime
    import logging
    import azure.functions as func
    from azure.identity import DefaultAzureCredential
    from azure.mgmt.resource import ResourceManagementClient
    def main(mytimer: func.TimerRequest) -> None:
        utc_timestamp = datetime.datetime.utcnow().replace(
        if mytimer.past_due:
  'The timer is past due!')
        # Get the resource group name from the environment variable
        resource_group_name = '{resource_group}'
        # Get the subscription ID from the environment variable
        subscription_id = '{subscription_id}'
        # Get the credential from the environment variable
        credential = DefaultAzureCredential()
        # Create the resource management client
        resource_client = ResourceManagementClient(credential, subscription_id)
        # Delete the resource group    
        poller = resource_client.resource_groups.begin_delete(resource_group_name)
        result = poller.result()
   "Deleted resource group %s.", resource_group_name)'Python timer trigger function ran at %s', utc_timestamp)

    PPUser's image

    PPPlease let me know if you have any questions.

    Please refer to the below documents for additional information -

    1. Delete a Resource Group using Python
    2. Example: Use the Azure libraries to create a resource group
    3. Key concepts - DefaultAzureCredential in local and on Azure
    4. Azure SDK - Python Samples
    5. Azure RBAC roles



    Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.