How to detach an OS hard disk from a VM before deletion

Bilocca Glenn 46 Reputation points
2023-10-31T07:25:50.5233333+00:00

Dear Community,

I would like to move a Vnet from a particular VM. Hence the only method that it is doable is via deletion of the VM and then get grasp of the disk and attach it to another particular VM. However when i tried to do so it automatically deleted the VM along with the disk as upon creation it was created like that.

I would like to detach the hard disk and i have tried to do it via Azure CLI following this command :

az vm disk detach -g myResourceGroup --vm-name myVm -n myOSDisk

However the following error is coming up :

The client 'xxxxxxxxxx@.xxx' with object id 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx' does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVm' or the scope is invalid.

Is there any other way how this can be done ?

Azure Disk Storage
Azure Disk Storage

A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 132.8K Reputation points MVP Volunteer Moderator
    2023-10-31T08:17:30.8133333+00:00

    Hi @Bilocca Glenn ,

    it's not possible to detach the OS disk of an Azure VM. The VM object can't "exist" without the OS disk object.

    Even this thread started 2019 it is still valid: Can OS disk be detached? #22501

    You can delete the VM and keep the OS disk. That's the only option I am aware of.

    This can be done in the Azure Portal by clicking on Delete and uncheck the option Delete with VM

    enter image description here

    In AZ CLI you can change the delete behavior of an existing Azure VM before you delete the VM:

    az resource update --resource-group myResourceGroup --name myVM --resource-type virtualMachines --namespace Microsoft.Compute --set properties.storageProfile.osDisk.deleteOption=detach
    

    Source: Update the delete behavior on an existing VM


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards Andreas Baumgarten

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

  2. Bilocca Glenn 46 Reputation points
    2023-10-31T08:57:09.11+00:00

    As attached below when selecting the test-vm(VM) and accept to delete it will remove both the VM and the disk.

    I don't have no option to untick the delete with the VM.User's image

    The following option Azure CLI is not working :

    az resource update --resource-group myResourceGroup --name myVM --resource-type virtualMachines --namespace Microsoft.Compute --set properties.storageProfile.osDisk.deleteOption=detach
    

    with the following error,

    The client 'xxxxxxxxxx@.xxx' with object id 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx' does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVm' or the scope is invalid.

    Was this answer helpful?


Your answer

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