Delete all VM’s in Azure Dev Test Labs

bharathn-msft 5,101 Reputation points Microsoft Employee
2019-10-29T21:29:59.16+00:00

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 DevTest Labs
Azure DevTest Labs
An Azure service that is used for provisioning development and test environments.
280 questions
0 comments No comments
{count} votes

Accepted answer
  1. kobulloc-MSFT 26,301 Reputation points Microsoft Employee
    2019-10-29T23:59:16.97+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.