Skip to content

Instantly share code, notes, and snippets.

@pve84
Last active October 3, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pve84/fa76a632babc9d9e60236f264cd3e164 to your computer and use it in GitHub Desktop.
Save pve84/fa76a632babc9d9e60236f264cd3e164 to your computer and use it in GitHub Desktop.
# This script will clean a Azure API Management resource.
# Be sure to run Connect-AzureRmAccount before running this script!
# Written by: Peter van Ekeren
# Date: 3-10-2018
param (
[bool]$DryRun = $false,
[bool]$RemoveProducts = $true,
[bool]$RemoveApis = $true,
[bool]$RemoveProperties = $true,
[Parameter(Mandatory=$true)][string]$ServiceName,
[Parameter(Mandatory=$true)][string]$ResourceGroupName
)
# Set the context
$context = New-AzureRmApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
# Remove subscriptions from products
function Remove-Subscriptions {
Write-Host("Removing Subscriptions")
$subscriptions = Get-AzureRmApiManagementSubscription -Context $context
foreach($s in $subscriptions){
Write-Host(" $($s.SubscriptionId)")
if($DryRun -eq $false){
Remove-AzureRmApiManagementSubscription -Context $context -SubscriptionId $s.SubscriptionId
}
}
}
function Remove-Products {
Write-Host("Removing Products")
$products = Get-AzureRmApiManagementProduct -Context $context
foreach($p in $products){
Write-Host(" $($p.ProductId)")
if($DryRun -eq $false){
Remove-AzureRmApiManagementProduct -Context $context -ProductId $p.ProductId
}
}
}
function Remove-Apis {
Write-Host("Removing Apis")
$apis = get-AzureRmApiManagementApi -Context $context
foreach($a in $apis){
Write-Host(" $($a.ApiId)")
if($DryRun -eq $false){
Remove-AzureRmApiManagementApi -Context $context -ApiId $a.ApiId
}
}
}
function Remove-Properties{
Write-Host("Removing Properties")
$properties = Get-AzureRmApiManagementProperty -Context $context
foreach($p in $properties){
Write-Host(" $($p.Name)")
if($DryRun -eq $false){
Remove-AzureRmApiManagementProperty -Context $context -PropertyId $p.PropertyId
}
}
}
Remove-Subscriptions
if($RemoveProducts){Remove-Products}
if($removeApis){Remove-Apis}
if($RemoveProperties){Remove-Properties}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment