This PowerShell script will help you to remove subnet from Microsoft Azure Virtual Network (vNET). You can follow my earlier articles regarding the another topic related to Azure networking here on Azure add create a Subnet to existing Virtual Network using PowerShell and Create a Azure Virtual Network with Subnet using PowerShell.
To remove subnet I have defined few variables in the PowerShell, regarding vNet name, Resource Group Name and Subnet which you want to remove from Azure vNET.
$vNetName = 'global_vnet_eastus' $resourceGroupName = 'vcloud-lab.com' $subnet = 'test'
Next create a config to remove subnet using the command Remove-AzVirtualNetworkSubnetConfig. This configuration is created in the memory and still not committed on actual Azure subnet.
Remove-AzVirtualNetworkSubnetConfig -Name $subnet -VirtualNetwork $azvNet
Name : global_vnet_eastus
ResourceGroupName : vcloud-lab.com
Location : eastus
Id : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus
Etag : W/"f358a336-2704-4097-a5b9-58fe5f7bfc48"
ResourceGuid : 65c12e07-56bb-4b6d-9316-fcf5d6c0604b
ProvisioningState : Succeeded
Tags :
AddressSpace : {
"AddressPrefixes": [
"10.10.0.0/16"
]
}
DhcpOptions : {
"DnsServers": []
}
FlowTimeoutInMinutes : null
Subnets : [
{
"Delegations": [],
"Name": "prod01-10.10.1.x",
"Etag": "W/\"f358a336-2704-4097-a5b9-58fe5f7bfc48\"",
"Id": "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus/subnets/prod01-10.10.1.x",
"AddressPrefix": [
"10.10.1.0/24"
],
"IpConfigurations": [],
"ServiceAssociationLinks": [],
"ResourceNavigationLinks": [],
"ServiceEndpoints": [],
"ServiceEndpointPolicies": [],
"PrivateEndpoints": [],
"ProvisioningState": "Succeeded",
"PrivateEndpointNetworkPolicies": "Enabled",
"PrivateLinkServiceNetworkPolicies": "Enabled",
"IpAllocations": []
}
]
VirtualNetworkPeerings : []
EnableDdosProtection : false
DdosProtectionPlan : null
ExtendedLocation : null
You can make permanent change using Set-AzVirtualNetwork which will push the configuration on Azure virtual network and delete the subnet.
$azvNet | Set-AzVirtualNetwork
Name : global_vnet_eastus
ResourceGroupName : vcloud-lab.com
Location : eastus
Id : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus
Etag : W/"147a06ff-3ef2-4850-ac71-873af02b399e"
ResourceGuid : 65c12e07-56bb-4b6d-9316-fcf5d6c0604b
ProvisioningState : Succeeded
Tags :
AddressSpace : {
"AddressPrefixes": [
"10.10.0.0/16"
]
}
DhcpOptions : {
"DnsServers": []
}
FlowTimeoutInMinutes : null
Subnets : [
{
"Delegations": [],
"Name": "prod01-10.10.1.x",
"Etag": "W/\"147a06ff-3ef2-4850-ac71-873af02b399e\"",
"Id": "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus/subnets/prod01-10.10.1.x",
"AddressPrefix": [
"10.10.1.0/24"
],
"IpConfigurations": [],
"ServiceAssociationLinks": [],
"ResourceNavigationLinks": [],
"ServiceEndpoints": [],
"ServiceEndpointPolicies": [],
"PrivateEndpoints": [],
"ProvisioningState": "Succeeded",
"PrivateEndpointNetworkPolicies": "Enabled",
"PrivateLinkServiceNetworkPolicies": "Enabled",
"IpAllocations": []
}
]
VirtualNetworkPeerings : []
EnableDdosProtection : false
DdosProtectionPlan : null
ExtendedLocation : null
Useful Articles
Deploy create Virtual Network vNET in Azure cloud using Ansible
Azure PowerShell Get-AzStorageShare Could not get the storage context
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Create an Azure Automation account on using Portal PowerShell and AzureCLI
Gather Azure Virtual Network inventory information using PowerShell
Get Azure virtual machine backup reports using Powershell
Why is my Azure recovery services vault not getting deleted?
Microsoft Azure PowerShell cmdlet 'this.Client.SubscriptionId' cannot be null
Azure PowerShell Get-AzStorageShare Could not get the storage context