question

bharathn-msft avatar image
0 Votes"
bharathn-msft asked bharathn-msft edited

Delete all VM’s in Azure Dev Test Labs

I am looking for an automated process of deleting all the VMs in my Azure Dev Test lab. Can anyone share the steps involved in this?

Sourced from FAQ

azure-devtestlabsazure-dtl-virtual-machinesazure-dtl-automation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

kobulloc-MSFT avatar image
1 Vote"
kobulloc-MSFT answered bharathn-msft edited

As a lab owner, you can delete VMs from your lab in the Azure portal. You also can delete all the VMs in your lab by using a PowerShell script. In the following example, under the values to change comment, modify the parameter values. You can retrieve the subscriptionId, labResourceGroup, and labName values from the lab pane in the Azure portal.

Please refer to below example , additional details please reference this documentation.

 # Delete all the VMs in a lab.
    
 # Values to change:
 $subscriptionId = ""
 $labResourceGroup = ""
 $labName = ""
    
 # Sign in to your Azure account.
 Connect-AzAccount
    
 # Select the Azure subscription that has the lab. This step is optional
 # if you have only one subscription.
 Select-AzSubscription -SubscriptionId $subscriptionId
    
 # Get the lab that has the VMs that you want to delete.
 $lab = Get-AzResource -ResourceId ('subscriptions/' + $subscriptionId + '/resourceGroups/' + $labResourceGroup + '/providers/Microsoft.DevTestLab/labs/' + $labName)
    
 # Get the VMs from that lab.
 $labVMs = Get-AzResource | Where-Object {
           $_.ResourceType -eq 'microsoft.devtestlab/labs/virtualmachines' -and
           $_.Name -like "$($lab.Name)/*"}
    
 # Delete the VMs.
 foreach($labVM in $labVMs)
 {
     Remove-AzResource -ResourceId $labVM.ResourceId -Force
 }



Sourced from FAQ



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.