Following on from my previous post about identifying API Connections which arent used we also used a script to clean up those connections too so that we can keep our environment healthy.

The below script is what we used.

Note that we have 1 resource group with everything in it for a given environment. This script is built around that constraint so please bear that in mind if you have a different setup.

#This will check for SAP API Connections and look at where they are pointing to
param (
    [string]$resourceGroupName = "mike-rg",
    [string]$subscriptionId = ''


#use the collection to build up objects for the table
$unusedConnectionList = New-Object "System.Collections.Generic.List[System.String]" 

$resourceGroupToLower = $resourceGroupName.ToLower()

Write-Host "Inputs" -ForegroundColor Green
Write-Host "======" -ForegroundColor Green
Write-Host 'Resource Group (to lower for kusto query): '  $resourceGroupToLower
Write-Host 'Subscription: '  $subscriptionId 
Write-Host 'Output Path: '  $outputPath

# Lookup API Connections
$apiConnectionsQuery = "resources
| where type == 'microsoft.web/connections'
| where resourceGroup == '$resourceGroupToLower'
| where subscriptionId == '$subscriptionId'
| project id, name"

Write-Host $apiConnectionsQuery

Write-Host "Query API Connections" -ForegroundColor Green
$apiConnectionsQueryResponse = Search-AzGraph $apiConnectionsQuery -First 1000
Write-Host $apiConnectionsQueryResponse.Count " items returned"

foreach($apiConnection in $apiConnectionsQueryResponse){
    $isConnectionUsed = $false

    Write-Host 'Processing API Connection' -ForegroundColor Green
    Write-Host '=========================' -ForegroundColor Green
    Write-Host 'Connection Name:' $apiConnection.Name
    Write-Host 'Connection Id:' $apiConnection.Id
    Write-Host ''

    $connectionId = $apiConnection.Id
    $connectionName = $apiConnection.Name

    $logicAppQuery = "resources
        | where type == 'microsoft.logic/workflows'
        | where resourceGroup == '$resourceGroupToLower'
        | where subscriptionId == '$subscriptionId'
        | where properties contains '$connectionId'
        | project id, name"

    $logicAppQueryResponse = Search-AzGraph $logicAppQuery -First 1000
    Write-Host $logicAppQueryResponse.Count " logic apps found with this api connection"

    foreach($logicApp in $logicAppQueryResponse){
        Write-Host $logicApp.Name ' uses: ' $connectionName
        $isConnectionUsed = $true

    if($isConnectionUsed -eq $false){

    Write-Host ''

Write-Host 'Remove Connections'  -ForegroundColor Green
Write-Host '=================='  -ForegroundColor Green
Write-Host 'About to remove unused API Connections'
Write-Host 'Connections identified to remove: ' $unusedConnectionList.Count

# Go through the identified unused resources and remove them
foreach($connectionId in $unusedConnectionList){
    Write-Host 'REMOVING RESOURCE: ' -ForegroundColor Green
    Write-Host $connectionId
    Write-Host ''

    Remove-AzResource -ResourceId $connectionId -Force   

Write-Host 'Complete - Unused API Connections cleaned up'  -ForegroundColor Green



Buy Me A Coffee