Skip to content

Instantly share code, notes, and snippets.

@notruri
Created September 7, 2025 11:53
Show Gist options
  • Select an option

  • Save notruri/228d8e61c63e56d305ee85e05b16cbb9 to your computer and use it in GitHub Desktop.

Select an option

Save notruri/228d8e61c63e56d305ee85e05b16cbb9 to your computer and use it in GitHub Desktop.
Purge an OCI (Oracle Cloud Infrastructure) VCN and its child resources.
#Requires -Version 7.0
<#
.SYNOPSIS
Purge an OCI (Oracle Cloud Infrastructure) VCN and its child resources.
.DESCRIPTION
Deletes route rules, subnets, internet gateways, then deletes the VCN.
.PARAMETER TargetName
The display name of the VCN to purge.
.PARAMETER CompartmentID
The OCID of the compartment containing the VCN.
.EXAMPLE
.\Purge-VCN.ps1 -TargetName "my-vcn" -CompartmentID "ocid1.compartment.oc1..aaaaaaa..."
#>
param(
[Parameter(Mandatory = $true)]
[string]$TargetName,
[Parameter(Mandatory = $true)]
[string]$CompartmentID
)
# Ensure OCI CLI exists
if (-not (Get-Command "oci" -ErrorAction SilentlyContinue)) {
Write-Error "OCI CLI not found in PATH. Please install/configure OCI CLI first."
exit 1
}
# Get VCNs
$vcns = oci network vcn list --all --compartment-id $CompartmentID | ConvertFrom-Json
$matchingVcns = $vcns.data | Where-Object { $_."display-name" -eq $TargetName }
if (-not $matchingVcns) {
Write-Warning "No VCNs found with display-name '$TargetName' in compartment $CompartmentID."
exit 0
}
foreach ($vcn in $matchingVcns) {
Write-Host "Processing VCN: $($vcn.id)"
# Clear route tables
$routeTables = oci network route-table list --all --vcn-id $vcn.id --compartment-id $($vcn."compartment-id") | ConvertFrom-Json
foreach ($rt in $routeTables.data) {
Write-Host " Clearing rules in route table: $($rt.id)"
oci network route-table update `
--rt-id $rt.id `
--route-rules "[]" `
--force *>&1 | Out-Null
}
# Delete subnets
$subnets = oci network subnet list --all --vcn-id $vcn.id --compartment-id $($vcn."compartment-id") | ConvertFrom-Json
foreach ($subnet in $subnets.data) {
Write-Host " Deleting subnet: $($subnet.id)"
oci network subnet delete `
--subnet-id $subnet.id `
--force `
--wait-for-state TERMINATED *>&1 | Out-Null
}
# Delete internet gateways
$gateways = oci network internet-gateway list --all --vcn-id $vcn.id --compartment-id $($vcn."compartment-id") | ConvertFrom-Json
foreach ($gateway in $gateways.data) {
Write-Host " Deleting internet gateway: $($gateway.id)"
oci network internet-gateway delete `
--ig-id $gateway.id `
--force `
--wait-for-state TERMINATED *>&1 | Out-Null
}
# Delete the VCN
Write-Host " Deleting VCN: $($vcn.id)"
oci network vcn delete `
--vcn-id $vcn.id `
--force `
--max-wait-seconds 60 `
--wait-for-state TERMINATED *>&1 | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment