Skip to content

Commit 68feda4

Browse files
authored
Merge pull request #171977 from cynthn/deep-delete
Deep delete
2 parents 686edb7 + 08281c2 commit 68feda4

File tree

4 files changed

+286
-9
lines changed

4 files changed

+286
-9
lines changed

articles/virtual-machines/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@
714714
href: ./windows/ps-template.md
715715
- name: Secure web server with TLS\SSL
716716
href: ./windows/tutorial-secure-web-server.md
717+
- name: Delete a VM and it's resources
718+
item: delete.md
717719
- name: Connect to Virtual Machines
718720
items:
719721
- name: Linux

articles/virtual-machines/delete.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
---
2+
title: Delete a VM and attached resources
3+
description: Learn how to delete a VM and the resources attached to the VM.
4+
author: cynthn
5+
ms.service: virtual-machines
6+
ms.subservice:
7+
ms.topic: how-to
8+
ms.workload: infrastructure
9+
ms.date: 11/19/2021
10+
ms.author: cynthn
11+
ms.custom: template-how-to
12+
13+
---
14+
15+
# Delete a VM and attached resources
16+
17+
By default, when you delete a VM it only deletes the VM resource, not the networking and disk resources. You can change this default behavior when you create a VM, or update an existing VM, to delete specific resources along with the VM.
18+
19+
20+
## Set delete options when creating a VM
21+
22+
### [CLI](#tab/cli2)
23+
24+
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. Resources that you `Detach`, like disks, will continue to incur charges as applicable.
25+
26+
- `--os-disk-delete-option` - OS disk.
27+
- `--data-disk-delete-option` - data disk.
28+
- `--nic-delete-option` - NIC.
29+
30+
In this example, we create a VM and set the OS disk and NIC to be deleted when we delete the VM.
31+
32+
```azurecli-interactive
33+
az vm create \
34+
--resource-group myResourceGroup \
35+
--name myVM \
36+
--image UbuntuLTS \
37+
--public-ip-sku Standard \
38+
--nic-delete-option delete \
39+
--os-disk-delete-option delete \
40+
--admin-username azureuser \
41+
--generate-ssh-keys
42+
```
43+
44+
### [PowerShell](#tab/powershell2)
45+
46+
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. Resources that you `Detach`, like disks, will continue to incur charges as applicable.
47+
48+
The `DeleteOption` parameters are:
49+
- `-OSDiskDeleteOption` - OS disk.
50+
- `-DataDiskDeleteOption` - data disk.
51+
- `-NetworkInterfaceDeleteOption` - NIC.
52+
53+
In this example, we create a VM and set the OS disk and NIC to be deleted when we delete the VM.
54+
55+
```azurepowershell
56+
New-AzVm `
57+
-ResourceGroupName "myResourceGroup" `
58+
-Name "myVM" `
59+
-OSDiskDeleteOption Delete `
60+
-NetworkInterfaceDeleteOption Delete `
61+
-Location "East US" `
62+
-VirtualNetworkName "myVnet" `
63+
-SubnetName "mySubnet" `
64+
-SecurityGroupName "myNetworkSecurityGroup" `
65+
-PublicIpAddressName "myPublicIpAddress"
66+
```
67+
68+
69+
### [REST](#tab/rest2)
70+
71+
This example shows how to set the data disk and NIC to be deleted when the VM is deleted.
72+
73+
```rest
74+
PUT
75+
https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/myVM?api-version=xx
76+
{
77+
"storageProfile": {
78+
"dataDisks": [
79+
{ "diskSizeGB": 1023,
80+
"name": "myVMdatadisk",
81+
"createOption": "Empty",
82+
"lun": 0,
83+
"deleteOption": “Delete”
84+
} ]
85+
},
86+
"networkProfile": {
87+
"networkInterfaces": [
88+
{ "id": "/subscriptions/.../Microsoft.Network/networkInterfaces/myNIC",
89+
"properties": {
90+
"primary": true,
91+
"deleteOption": “Delete”
92+
} }
93+
]
94+
}
95+
```
96+
97+
98+
You can also set this property for a Public IP associated with a NIC, so that the Public IP is automatically deleted when the NIC gets deleted.
99+
100+
```rest
101+
PUT https://management.azure.com/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/networkInterfaces/test-nic?api-version=xx
102+
{
103+
104+
"properties": {
105+
106+
"enableAcceleratedNetworking": true,
107+
108+
"ipConfigurations": [
109+
110+
{
111+
112+
"name": "ipconfig1",
113+
114+
"properties": {
115+
116+
"publicIPAddress": {
117+
118+
"id": "/subscriptions/../publicIPAddresses/test-ip",
119+
120+
          "properties": {
121+
            “deleteOption”: “Delete”
122+
}
123+
},
124+
125+
"subnet": {
126+
127+
"id": "/subscriptions/../virtualNetworks/rg1-vnet/subnets/default"
128+
129+
}
130+
131+
}
132+
133+
}
134+
135+
]
136+
137+
},
138+
139+
"location": "eastus"
140+
141+
}
142+
```
143+
---
144+
145+
146+
## Update the delete behavior on an existing VM
147+
148+
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.
149+
150+
151+
```rest
152+
PATCH https://management.azure.com/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/virtualMachines/testvm?api-version=2021-07-01
153+
154+
155+
{
156+
"properties": {
157+
"hardwareProfile": {
158+
"vmSize": "Standard_D2s_v3"
159+
},
160+
"storageProfile": {
161+
"imageReference": {
162+
"publisher": "MicrosoftWindowsServer",
163+
"offer": "WindowsServer",
164+
"sku": "2019-Datacenter",
165+
"version": "latest",
166+
"exactVersion": "17763.3124.2111130129"
167+
},
168+
"osDisk": {
169+
"osType": "Windows",
170+
"name": "OsDisk_1",
171+
"createOption": "FromImage",
172+
"caching": "ReadWrite",
173+
"managedDisk": {
174+
"storageAccountType": "Premium_LRS",
175+
"id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/OsDisk_1"
176+
},
177+
"deleteOption": "Delete",
178+
"diskSizeGB": 127
179+
},
180+
"dataDisks": [
181+
{
182+
"lun": 0,
183+
"name": "DataDisk_0",
184+
"createOption": "Attach",
185+
"caching": "None",
186+
"writeAcceleratorEnabled": false,
187+
"managedDisk": {
188+
"storageAccountType": "Premium_LRS",
189+
"id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/DataDisk_0"
190+
},
191+
"deleteOption": "Delete",
192+
"diskSizeGB": 1024,
193+
"toBeDetached": false
194+
},
195+
{
196+
"lun": 1,
197+
"name": "DataDisk_1",
198+
"createOption": "Attach",
199+
"caching": "None",
200+
"writeAcceleratorEnabled": false,
201+
"managedDisk": {
202+
"storageAccountType": "Premium_LRS",
203+
"id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Compute/disks/DataDisk_1"
204+
},
205+
"deleteOption": "Delete",
206+
"diskSizeGB": 1024,
207+
"toBeDetached": false
208+
}
209+
]
210+
},
211+
"osProfile": {
212+
"computerName": "testvm",
213+
"adminUsername": "azureuser",
214+
"windowsConfiguration": {
215+
"provisionVMAgent": true,
216+
"enableAutomaticUpdates": true,
217+
"patchSettings": {
218+
"patchMode": "AutomaticByOS",
219+
"assessmentMode": "ImageDefault",
220+
"enableHotpatching": false
221+
}
222+
},
223+
"secrets": [],
224+
"allowExtensionOperations": true,
225+
"requireGuestProvisionSignal": true
226+
},
227+
"networkProfile": {
228+
"networkInterfaces": [
229+
{
230+
"id": "/subscriptions/subID/resourceGroups/resourcegroup/providers/Microsoft.Network/networkInterfaces/nic336"
231+
,
232+
"properties": {
233+
"deleteOption": "Delete"
234+
}
235+
}
236+
]
237+
}
238+
}
239+
}
240+
```
241+
242+
## FAQ
243+
244+
### Q: Does this feature work with shared disks?
245+
246+
A: For shared disks, you cannot set the ‘deleteOption’ property to ‘Delete’. You can leave it blank or set it to ‘Detach’
247+
248+
249+
### Q: Which Azure resources support this feature?
250+
251+
A: This feature is supported on all managed disk types used as OS disks and Data disks, NICs, and Public IPs
252+
253+
254+
### Q: Can I use this feature on disks and NICs that are not associated with a VM?
255+
256+
A: No, this feature is only available on disks and NICs associated with a VM.
257+
258+
259+
### Q: How does this feature work with Flexible virtual machine scale sets?
260+
261+
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.
262+
263+
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.
264+
265+
266+
### Q: Do Spot VMs support this feature?
267+
268+
A: Yes, you can use this feature for Spot VMs just the way you would for on-demand VMs.
269+
270+
271+
### Q: How do I persist the disks, NIC, and Public IPs associated with a VM?
272+
273+
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 have the option to revert so that these resources are persisted after the VM is deleted. To persist these resources, set the `deleteOption` property to `Detach` and then these resources will then be persisted when the VM is deleted.
274+
275+
276+
## Next steps
277+
278+
To learn more about basic VM management, see [Tutorial: Create and Manage Linux VMs with the Azure CLI](linux/tutorial-manage-vm.md).

articles/virtual-machines/linux/tutorial-manage-vm.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ az vm stop --resource-group myResourceGroupVM --name myVM
299299
az vm start --resource-group myResourceGroupVM --name myVM
300300
```
301301

302-
### Delete resource group
302+
### Deleting VM resources
303+
304+
You can delete a VM, but by default this only deletes the VM resource, not the disks and networking resources the VM uses. You can change the default behavior to delete other resources when you delete the VM. For more information, see [Delete a VM and attached resources](../delete.md).
303305

304306
Deleting a resource group also deletes all resources contained within, such as the VM, virtual network, and disk. The `--no-wait` parameter returns control to the prompt without waiting for the operation to complete. The `--yes` parameter confirms that you wish to delete the resources without an additional prompt to do so.
305307

articles/virtual-machines/windows/tutorial-manage-vm.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ms.topic: tutorial
77
ms.workload: infrastructure
88
ms.date: 06/06/2019
99
ms.author: cynthn
10+
ms.collection: windows
1011
ms.custom: mvc, devx-track-azurepowershell
1112

1213
#Customer intent: As an IT administrator, I want to learn about common maintenance tasks so that I can create and manage Windows VMs in Azure
@@ -286,15 +287,9 @@ Start-AzVM `
286287
-Name "myVM"
287288
```
288289

289-
### Delete resource group
290+
### Deleting VM resources
290291

291-
Everything inside of a resource group is deleted when you delete the resource group.
292-
293-
```azurepowershell-interactive
294-
Remove-AzResourceGroup `
295-
-Name "myResourceGroupVM" `
296-
-Force
297-
```
292+
You can delete a VM, but by default this only deletes the VM resource, not the disks and networking resources the VM uses. You can change the default behavior to delete other resources when you delete the VM. For more information, see [Delete a VM and attached resources](../delete.md).
298293

299294
## Next steps
300295

0 commit comments

Comments
 (0)