Edit

Share via


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

  1. Open the portal.

  2. Select + Create a resource.

  3. On the Create a resource page, under Virtual machines, select Create.

  4. Make your choices on the Basics, then select Next : Disks > to open the Disks tab.

  5. Under Disk options, by default the OS disk is set to Delete with VM. If you don't want to delete the OS disk, clear the checkbox. If you're using an existing OS disk, the default is to detach the OS disk when the VM is deleted.

    Screenshot checkbox to choose to have the disk deleted when the VM is deleted.

  6. Under Data disks, you can either attach an existing data disk or create a new disk and attach it to the VM.

    • If you choose Create and attach a new disk, the Create a new disk page opens and you can select whether to delete the disk when you delete the VM. Screenshot showing a checkbox to choose to delete the data disk when the VM is deleted.

    • If you choose to Attach an existing disk, can choose the disk, LUN, and whether you want to delete the data disk when you delete the VM. Screenshot showing the checkbox to choose to delete the data disk when the VM is deleted.

  7. When you're done adding your disk information, select Next : Networking > to open the Networking tab.

  8. Towards the bottom of the page, select Delete public IP and NIC when VM is deleted.

    Screenshot showing the checkbox to choose to delete the public IP and NIC when the VM is deleted.

  9. When you're done making selections, select Review + create.

  10. You can verify which resources you have chosen to delete when you delete the VM.

  11. When you're satisfied with your selections, and validation passes, select Create to deploy the VM.

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: Do Spot VMs support this feature?

A: Yes, you can use this feature for Spot VMs just the way you would for on-demand VMs.

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.