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:
- Get the VM's current properties.
- Remove the data disk from the VM's properties.
- 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.