Delete a VM and attached resources

Depending on how you delete a VM, it may only delete the VM resource, not the networking and disk resources. You can change the default settings for what other resources are deleted when you delete a VM.

Set delete options when creating a VM

To specify what happens to the attached resources when you delete a VM, use the delete-option parameters. Each can be set to either Delete, which permanently deletes the resource when you delete the VM, or Detach which only detaches the resource and leaves it in Azure so it can be reused later. The default for VMs created using the CLI is to detach the OS disk. Resources that you Detach, like disks, will continue to incur charges as applicable.

  • --os-disk-delete-option - OS disk.
  • --data-disk-delete-option - data disk.
  • --nic-delete-option - NIC.

In this example, we create a VM named myVM in the resource group named myResourceGroup using an image named myImage, and set the OS disk and NIC to be deleted when we delete the VM.

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image myImage \
    --public-ip-sku Standard \
    --nic-delete-option delete \
    --os-disk-delete-option delete \
    --admin-username azureuser \
    --generate-ssh-keys

Update the delete behavior on an existing VM

You can change the behavior when you delete a VM.

The following example sets the delete option to detach so you can reuse the disk.

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

Force Delete for VMs

Force delete allows you to forcefully delete your virtual machine, reducing delete latency and immediately freeing up attached resources. For VMs that don't require graceful shutdown, Force Delete will delete the VM as fast as possible while relieving the logical resources from the VM, bypassing the graceful shutdown and some of the cleanup operations. Force Delete won't immediately free the MAC address associated with a VM, as this is a physical resource that may take up to 10 min to free. If you need to immediately reuse the MAC address on a new VM, Force Delete isn't recommended. Force delete should only be used when you aren't intending to reuse virtual hard disks. You can use force delete through Portal, CLI, PowerShell, and REST API.

When you go to delete an existing VM, you'll find an option to apply force delete in the delete pane.

  1. Open the portal.
  2. Navigate to your virtual machine.
  3. On the Overview page, select Delete.
  4. In the Delete virtual machine pane, select the checkbox for Apply force delete.
  5. Select Ok.

Force Delete for scale sets

Force delete allows you to forcefully delete your Virtual Machine Scale Set, reducing delete latency and immediately freeing up attached resources. Force Delete won't immediately free the MAC address associated with a VM, as this is a physical resource that may take up to 10 min to free. If you need to immediately reuse the MAC address on a new VM, Force Delete is not recommended. Force delete should only be used when you are not intending to reuse virtual hard disks. You can use force delete through Portal, CLI, PowerShell, and REST API.

When you go to delete an existing scale set, you'll find an option to apply force delete in the delete pane.

  1. Open the portal.
  2. Navigate to your Virtual Machine Scale Set.
  3. On the Overview page, select Delete.
  4. In the Delete Virtual Machine Scale Set pane, select the checkbox for Apply force delete.
  5. Select Ok.

FAQ

Q: Does this feature work with shared disks?

A: For shared disks, you can't set the 'deleteOption' property to 'Delete'. You can leave it blank or set it to 'Detach'

Q: Which Azure resources support this feature?

A: This feature is supported on all managed disk types used as OS disks and Data disks, NICs, and Public IPs

Q: Can I use this feature on disks and NICs that aren't associated with a VM?

A: No, this feature is only available on disks and NICs associated with a VM.

Q: How does this feature work with Flexible Virtual Machine Scale Set?

A: For Flexible Virtual Machine Scale Set the disks, NICs, and PublicIPs have deleteOption set to Delete by default so these resources are automatically cleaned up when the VMs are deleted.

For data disks that were explicitly created and attached to the VMs, you can modify this property to ‘Detach’ instead of ‘Delete’ if you want the disks to persist after the VM is deleted.

Q: How do I persist the disks, NIC, and Public IPs associated with a VM?

A: By default, disks, NICs, and Public IPs associated with a VM are persisted when the VM is deleted. If you configure these resources to be automatically deleted, you can update the settings so that the resources remain after the VM is deleted. To keep these resources, set the deleteOption property to Detach.

Next steps

To learn more about basic VM management, see Tutorial: Create and Manage Linux VMs with the Azure CLI.