Hi @Venkata satyanarayana Machari veera (CIS) ,
Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.
I understand that you are looking for a way to delete the orphaned NICs.
I see @Pierre-Luc Giguere has shared the Microsoft Learn on how to achieve this.
https://learn.microsoft.com/en-us/previous-versions/azure/virtual-machines/linux/find-unattached-nics
The only challenge in the mentioned CLI script is that this will also list the NICs associated with private End Point.
Anyways, it will throw an error and will not delete these NICs (private endPoint), and will continue to delete the rest of the NICs
If you rarely have to clean up the NICs, you can go ahead with the CLI script.
Now, if you don't want to list the private EndPoint NICs, you can use the below PowerShell script.
To View the NICs
$nics=Get-AzNetworkInterface -ResourceGroupName "YourResourceGroupName" | Where-Object {($.Name -notmatch '.nic.') -and ($.VirtualMachine -eq $null)}
$WarningPreference = 'SilentlyContinue'
$nics | Format-Table -Property Name, ResourceGroupName, Location, Id
To Delete the NICs
$nics=Get-AzNetworkInterface -ResourceGroupName "YourResourceGroupName" | Where-Object {($.Name -notmatch '.nic.') -and ($.VirtualMachine -eq $null)}
$WarningPreference = 'SilentlyContinue'
foreach ($nic in $nics)
{
$WarningPreference = 'SilentlyContinue'
Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $nic.ResourceGroupName
}
Reference :
https://learn.microsoft.com/en-us/powershell/module/az.network/remove-aznetworkinterface?view=azps-8.1.0
P.S :
- The script works on the fact that Private EndPoint NICs, by default have ".nic." on their name.
- If you are creating a NIC that has ".nic." in it's name, then the above script will omit this NIC in the listing.
- However, I do not think this would be the case for the majority of the times
Kindly let me know if this helps, In case there are follow-up questions, please do let us know.
Thanks,
Kapil
----------------------------------------------------------------------------------------------------------------
Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.