azurepowershellazure-cliazure-eventgrid

remove event subscriptions from system topics


my powershell code is so slow to delete all the event subscriptins from the system topic.

# Get all event subscription names associated with the system topic
$subscriptions = az eventgrid system-topic event-subscription list --system-topic-name $systemTopicName --resource-group $resourceGroupName --query "[].name" -o tsv

# Iterate through each subscription and delete it
foreach ($sub in $subscriptions) {
    Write-Host "Deleting event subscription: $sub"
    az eventgrid system-topic event-subscription delete --system-topic-name $systemTopicName --resource-group $resourceGroupName --name $sub --yes
}

Is there any faster way to remove all the event subscriptions?


Solution

  • ForEach-Object -Parallel is available from Powershell v7.0 and can be used to parallelize work.

    Try something like this (not tested):

    # Get all event subscription names associated with the system topic
    $subscriptions = az eventgrid system-topic event-subscription list --system-topic-name $systemTopicName --resource-group $resourceGroupName --query "[].name" -o tsv
    
    # Iterate through each subscription and delete it
    $subscriptions | Foreach-Object -Parallel {
        Write-Host "Deleting event subscription: $_"
    
        az eventgrid system-topic event-subscription delete --system-topic-name $systemTopicName --resource-group $resourceGroupName --name $_ --yes
    } -ThrottleLimit 5
    

    Please note that:

    See ForEach-Object for more details and examples.