Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
- Open the portal.
- Navigate to your virtual machine.
- On the Overview page, select Delete.
- In the Delete virtual machine pane, select the checkbox for Apply force delete.
- 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.
- Open the portal.
- Navigate to your Virtual Machine Scale Set.
- On the Overview page, select Delete.
- In the Delete Virtual Machine Scale Set pane, select the checkbox for Apply force delete.
- 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: What happens to the data on the disks when a VM is deleted?
A: When a VM is deleted, the data on the disks is not automatically deleted. The OS disk and any attached data disks remain in your resource group and continue to incur charges. You need to manually delete these disks if you no longer need them.
Q: What happens to VM backups when I delete the VM?
A: Azure Backup retains VM backups according to the backup policy, even after the VM is deleted. Retained backups continue to incur storage charges until they are deleted. To avoid unnecessary costs, review your backup retention and delete backups you no longer need.
To manage or delete VM backups, go to the Azure portal, navigate to your Recovery Services vault, select Backup items, then select Azure Virtual Machine, and choose the VM whose backups you want to manage. From there, you can stop backup and delete backup data as needed.
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.