Is it possible to detach a data disk from a Azure VM using Rest API. If yes, can u please share the URL and payload.

Puru Kumar 20 Reputation points
2023-04-21T17:20:04.4133333+00:00

I'm currently developing a Python script to attach and detach disks from an Azure VM. While I've been successful in attaching the disk, I'm encountering issues with detaching it. Kindly provide the appropriate REST API URL and payload that would allow me to achieve this task.

My code:

detach_disk_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}?api-version=2021-07-01"

headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}

data = {
    'location': location,
    'properties': {
        'storageProfile': {
            'dataDisks': [
                {
                    'name': disk_name,
                    'detachOption': 'ForceDetach'
                }
            ]
        }
    }
}

response = requests.put(detach_disk_url, headers=headers, json=data)
Azure Disk Storage
Azure Disk Storage
A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
668 questions
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 38,021 Reputation points Microsoft Employee Moderator
    2023-04-24T15:19:15.57+00:00

    @Puru Kumar

    Yes, you can detach a data disk from an Azure VM using the Azure REST API. To do this, you need to perform the following steps:

    1. Get the VM's current properties.
    2. Remove the data disk from the VM's properties.
    3. Update the VM with the modified properties.

    Here is some sample code that might help:

    # Step 1: Get the VM's current properties
    get_vm_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}?api-version=2021-07-01"
    
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json'
    }
    
    response = requests.get(get_vm_url, headers=headers)
    vm_properties = response.json()
    
    # Step 2: Remove the data disk from the VM's properties
    data_disks = vm_properties['properties']['storageProfile']['dataDisks']
    data_disks[:] = [disk for disk in data_disks if disk['name'] != disk_name]
    
    # Step 3: Update the VM with the modified properties
    update_vm_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}?api-version=2021-07-01"
    
    response = requests.put(update_vm_url, headers=headers, json=vm_properties)
    

    This code first retrieves the VM's current properties, then removes the data disk with the specified disk_name from the dataDisks array, and finally updates the VM with the modified properties. Make sure to replace the variables with your subscription ID, resource group name, VM name, disk name, location, and access token.

    Hope this helps. Let me know if you need further assistance.


    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.