get public-ip ids and delete with associated resources Virtual mashine in Azure (With Azure cli or Bash)

Aleksandr Slypchenko 40 Reputation points
2023-03-24T10:19:26.4166667+00:00

How get public-ip ids and delete with associated resources Virtual mashine in Azure (With Azure cli or Bash) ?

$nics= (az vm show --resource-group $myresource-group --name $Name-vm --subscription $subscr --query "networkProfile.networkInterfaces[].id" --output tsv)
foreach ($nic in $nics){
   $publicIps=az network nic show --id $nic --query "ipConfigurations[].publicIpAddress.id" --output tsv
}
echo $publicIps

************************************************************************************************************

# Get the public IP IDs associated with the virtual machine
publicIpIds=$(az vm show -g $resourceGroup -n $vmName --subscription $subscr --query 'publicIps[*].id' -o tsv)

# Loop through the public IP IDs and delete them
for id in $publicIpIds
do
    az network public-ip delete --ids $id
done

Not Work
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,789 questions
Azure Kubernetes Service (AKS)
Azure Kubernetes Service (AKS)
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,073 questions
{count} votes

Accepted answer
  1. shiva patpi 13,251 Reputation points Microsoft Employee
    2023-03-24T17:12:00.3533333+00:00

    Hello @Aleksandr Slypchenko ,

    Please use -d option in az vm show command

    Example command for my VM:

    az vm show -d -g cks -n worker1 --query publicIps -o tsv

    See the screen shots of the difference when you use -d option and with out that option:

    User's image

    Regards,

    Shiva.


2 additional answers

Sort by: Most helpful
  1. Aleksandr Slypchenko 40 Reputation points
    2023-03-27T10:19:19.67+00:00

    Thank you.

    Yes it works.

    The question is closed

    0 comments No comments

  2. Aleksandr Slypchenko 40 Reputation points
    2023-03-27T10:42:57.07+00:00

    Thank you.

    Yes it works.

    0 comments No comments

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.