You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何快速批量删除Azure Event Grid系统主题的事件订阅?

更快删除Event Grid系统主题事件订阅的方法

原代码执行缓慢的核心原因是逐个串行发起删除请求,每个az命令都会单独建立API连接、处理请求,叠加了网络延迟和CLI启动开销。以下是两种高效优化方案:

方案1:PowerShell并行执行(适用于PowerShell 7+)

利用ForEach-Object -Parallel同时发起多个删除请求,大幅压缩总耗时:

# 获取所有事件订阅名称
$subscriptions = az eventgrid system-topic event-subscription list --system-topic-name $systemTopicName --resource-group $resourceGroupName --query "[].name" -o tsv

# 并行删除订阅(可调整-ThrottleLimit控制并发数)
$subscriptions | ForEach-Object -Parallel {
    $sub = $_
    $systemTopicName = $using:systemTopicName
    $resourceGroupName = $using:resourceGroupName
    Write-Host "Deleting event subscription: $sub"
    az eventgrid system-topic event-subscription delete --system-topic-name $systemTopicName --resource-group $resourceGroupName --name $sub --yes --output none
} -ThrottleLimit 10

方案2:后台作业兼容(适用于PowerShell 5.1+)

如果仍在使用PowerShell 5.1,可通过Start-Job实现并行处理:

# 获取所有事件订阅名称
$subscriptions = az eventgrid system-topic event-subscription list --system-topic-name $systemTopicName --resource-group $resourceGroupName --query "[].name" -o tsv

# 启动后台作业批量删除
$jobs = $subscriptions | ForEach-Object {
    Start-Job -ScriptBlock {
        param($sub, $systemTopicName, $resourceGroupName)
        Write-Host "Deleting event subscription: $sub"
        az eventgrid system-topic event-subscription delete --system-topic-name $systemTopicName --resource-group $resourceGroupName --name $sub --yes --output none
    } -ArgumentList $_, $systemTopicName, $resourceGroupName
}

# 等待所有作业完成并清理
$jobs | Wait-Job | Receive-Job
$jobs | Remove-Job

额外提速技巧

  • 控制并发数:根据Azure API限流规则调整-ThrottleLimit或后台作业数量,避免触发请求限制。
  • 关闭冗余输出:添加--output none参数减少CLI输出处理开销,进一步提升速度。

内容的提问来源于stack exchange,提问作者Greencolor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:52:02