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!
Original Article Source: Azure CLI: Delete Public IP from Existing NIC / VM written by Chris Pietschmann (If you’re reading this somewhere other than Build5Nines.com, it was republished without permission.)