Sign in
agent:

Delete Azure VM Instances

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task deletes specified virtual machines from Azure's cloud services. This process frees up associated resources and halts related expenses. It's crucial for optimizing resource usage, but it's irreversible, necessitating careful decision-making.

from azure.identity import DefaultAzureCredential from azure.mgmt.compute import ComputeManagementClient from azure.core.exceptions import HttpResponseError # Initialize Azure credentials credential = DefaultAzureCredential() # Initialize ComputeManagementClient compute_client = ComputeManagementClient(credential, subscription_id) # vm_name_to_delete = "test-vm-1" # Name of the VM to be deleted # To be initialized in the input parameters try: # Find the VM to delete based on the name vm_to_delete = next((vm for vm in idle_vms if vm.name == vm_name_to_delete), None) if vm_to_delete: # Extracting resource group name from the VM's ID resource_group_name = vm_to_delete.id.split('/')[4] print(f"Initiating deletion of VM: {vm_name_to_delete} in Resource Group: {resource_group_name}") # Begin deletion of the specified VM delete_operation = compute_client.virtual_machines.begin_delete(resource_group_name, vm_name_to_delete) delete_operation.wait() # Wait for the delete operation to complete print(f"VM {vm_name_to_delete} has been successfully deleted.") else: print(f"No VM found with the name '{vm_name_to_delete}' in the subscription.") except HttpResponseError as e: print(f"An error occurred with the Azure HTTP response: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
copied