Delete Azure virtual network address space using powershell

Martin Thong 60 Reputation points
2024-04-15T10:18:18.7333333+00:00

I have a virtual network that have multiple address spaces. I want to use powershell to delete a specific address space.

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,427 questions
0 comments No comments
{count} votes

Accepted answer
  1. Patchfox 3,921 Reputation points
    2024-04-15T17:00:27.9566667+00:00

    Hi Martin Thong, I want to help you with this question.

    When I understand you correctly, you want to delete a network address space (including every subnet in the address space), right?

    If you want to remove only a specific address space within the virtual network, you’ll need to update the address space configuration. Unfortunately, there isn’t a direct cmdlet to remove just one address space. Instead, you can:

    • Update the virtual network configuration to exclude the specific address space.
    • Delete the existing virtual network and create a new one without the unwanted address space.

    There is no specific PowerShell cmdlet available to do that action.

    But you can do the following for example:

    Remove Subnets in the Address Range:

    If any subnets exist within the address range you want to remove, you must delete those subnets first. Use the Remove-AzVirtualNetworkSubnetConfig cmdlet to remove a subnet from the virtual network.

    For example, if you have a subnet named “Subnet1” within the address range you want to remove, use the following command:

    Remove-AzVirtualNetworkSubnetConfig
    

    Update the Address Space:

    Use the Get-AzVirtualNetwork cmdlet to retrieve the current configuration of the virtual network.

    Add or remove address prefixes as needed. For example, if your existing virtual network has an address prefix of 10.0.0.0/16, you can extend it with additional address prefixes using the following commands:

    $existingVNet = Get-AzVirtualNetwork -ResourceGroupName "myResourceGroup" -Name "myVNet"
    $updatedVNet = $existingVNet | Set-AzVirtualNetwork -AddressPrefix @("10.0.0.0/16", "192.168.0.0/24")
    

    Apply the Changes:

    Finally, apply the updated configuration to the virtual network using the Set-AzVirtualNetwork cmdlet:

    
     Set-AzVirtualNetwork
    
    

    I hope this will give you a sufficient solution


    If the reply was helpful, please don’t forget to upvote or accept it as an answer, thank you!

    1 person found this answer helpful.

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.