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 >. The Disks tab will open.

  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 will open 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, you'll be able to 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 >. The Networking tab will open.

  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. The Review + create page will open.

  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 use the Azure REST API to patch a VM to change the behavior when you delete a VM. The following example updates the VM to delete the NIC, OS disk, and data disk when the VM is deleted.

PATCH https://management.azure.com/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/virtualMachines/testvm?api-version=2021-07-01 


{ 
    "properties": {        
        "hardwareProfile": { 
            "vmSize": "Standard_D2s_v3" 
        }, 
        "storageProfile": { 
            "imageReference": { 
                "publisher": "MicrosoftWindowsServer", 
                "offer": "WindowsServer", 
                "sku": "2019-Datacenter", 
                "version": "latest", 
                "exactVersion": "17763.3124.2111130129" 
            }, 
            "osDisk": { 
                "osType": "Windows", 
                "name": "OsDisk_1", 
                "createOption": "FromImage", 
                "caching": "ReadWrite", 
                "managedDisk": { 
                    "storageAccountType": "Premium_LRS", 
                    "id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/OsDisk_1" 
                }, 
                "deleteOption": "Delete", 
                "diskSizeGB": 127 
            }, 
            "dataDisks": [ 
                { 
                    "lun": 0, 
                    "name": "DataDisk_0", 
                    "createOption": "Attach", 
                    "caching": "None", 
                    "writeAcceleratorEnabled": false, 
                    "managedDisk": { 
                        "storageAccountType": "Premium_LRS", 
                        "id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/DataDisk_0" 
                    }, 
                    "deleteOption": "Delete", 
                    "diskSizeGB": 1024, 
                    "toBeDetached": false 
                }, 
                { 
                    "lun": 1, 
                    "name": "DataDisk_1", 
                    "createOption": "Attach", 
                    "caching": "None", 
                    "writeAcceleratorEnabled": false, 
                    "managedDisk": { 
                        "storageAccountType": "Premium_LRS", 
                        "id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/DataDisk_1" 
                    }, 
                    "deleteOption": "Delete", 
                    "diskSizeGB": 1024, 
                    "toBeDetached": false 
                } 
            ] 
        }, 
        "osProfile": { 
            "computerName": "testvm", 
            "adminUsername": "azureuser", 
            "windowsConfiguration": { 
                "provisionVMAgent": true, 
                "enableAutomaticUpdates": true, 
                "patchSettings": { 
                    "patchMode": "AutomaticByOS", 
                    "assessmentMode": "ImageDefault", 
                    "enableHotpatching": false 
                } 
            }, 
            "secrets": [], 
            "allowExtensionOperations": true, 
            "requireGuestProvisionSignal": true 
        }, 
        "networkProfile": { 
            "networkInterfaces": [ 
                { 
                    "id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Network/networkInterfaces/nic336" 
                , 
                   "properties": { 
                   "deleteOption": "Delete" 
} 
} 
            ] 
        } 
} 
} 

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 do not require graceful shutdown, Force Delete aims to delete the VM as fast as possible while relieving the logical resources from the VM, bypassing some of the cleanup operations. Force delete should only be used when you are not intending to re-use virtual hard disks. You can use force delete through Portal, CLI, PowerShell, and REST API.

When you go to delete an existing VM, you will 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 virtual machine scale sets

Force delete allows you to forcefully delete your Uniform virtual machine scale sets, reducing delete latency and immediately freeing up attached resources. Force delete should only be used when you are not intending to re-use virtual hard disks. You can use force delete through Portal, CLI, PowerShell, and REST API.

When you go to delete an existing virtual machine scale set, you will 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 sets?

A: For Flexible virtual machine scale sets 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.