So you have already created a Virtual Machine in Azure that is provisioned with a Public IP address and you need to remove it. Don’t worry, you do NOT need to delete the VM. You can disassociate the Public IP from the Network Interface connected to the VM, and then delete the Public IP. This can easily be done using the Azure CLI.

Below is a simple script that will use the az network nic ip-config update command to disassociate the Public IP from the Network Interface so it’s no longer assigned and being used. Then, the script will use the az network public-ip delete command to delete the Public IP since it’s no longer needed anymore.

resourceGroupName="myRG"
nicName="myVMNic"
publicIp="myPublicIp"
publicIpNameOnNic="ipconfig1"

# Disassociate the Public IP Address from the NIC first
az network nic ip-config update \
  --resource-group $resourceGroupName --nic-name $nicName \
  --name $publicIpNameOnNic --remove PublicIpAddress

# Delete the Public IP
az network public-ip delete -g $resourceGroupName -n $publicIp

Keep in mind that if you don’t want to delete the Public IP, but only disassociate it from the Network Interface, you could just do that first step. Then you’ll have a disconnected Public IP resource within your resource group. This may be desired if you have the Public IP to have a static IP Address that you don’t want to lose. After all, if the Public IP address has a static IP Address configured, you will lose control / access to that Public IP Address by deleting the Public IP resource itself.

Happy scripting!

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading