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.
On the Create a resource page, under Virtual machines, select Create.
Make your choices on the Basics, then select Next : Disks > to open the Disks tab.
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.
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.
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.
When you're done adding your disk information, select Next : Networking > to open the Networking tab.
Towards the bottom of the page, select Delete public IP and NIC when VM is deleted.
When you're done making selections, select Review + create.
You can verify which resources you have chosen to delete when you delete the VM.
When you're satisfied with your selections, and validation passes, select Create to deploy the 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
To specify what happens to the attached resources when you delete a VM, use the DeleteOption 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 PowerShell is for the OS disk to be detached when you delete the VM. Resources that you Detach, like disks, will continue to incur charges as applicable.
The DeleteOption parameters are:
-OSDiskDeleteOption - OS disk.
-DataDiskDeleteOption - data disk.
-NetworkInterfaceDeleteOption - NIC.
In this example, we create a VM and set the OS disk and NIC to be deleted when we delete the VM.
This example shows how to set the data disk and NIC to be deleted when the VM is deleted. Note, the API version specified in the api-version parameter must be '2021-03-01' or newer to configure the delete option.
The following example updates the VM to delete the NIC, OS disk, and data disk when the VM is deleted. Note, the API version specified in the api-version parameter must be '2021-03-01' or newer to configure the delete option.
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.
You can use the Azure REST API to apply force delete to your virtual machines. Use the forceDeletion parameter for Virtual Machines - Delete.
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.
You can use the Azure REST API to apply force delete to your scale set. Use the forceDeletion parameter for Virtual Machines Scale Sets - Delete.
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.