Deleting Foundry Agents in Bulk with C#

• Foundry, Agents, Bulk Deletion, Automation • 5 min read

The Problem

If you’ve worked with Azure AI Foundry Agents (formerly Azure AI Projects), you might have found yourself in a situation where you need to clean up multiple test agents. While the Azure portal provides a UI for managing agents, deleting them one by one is tedious and time-consuming.

I initially attempted to delete multiple agents in bulk using the az rest command to call the Azure AI Foundry API directly, but ran into various authentication and API compatibility issues. After experiencing the frustration of manually clicking through the portal to delete agents one at a time during development iterations, I decided to create a simple tool to streamline the process.

This post walks through a .NET file-based app that can list and delete all agents in your Azure AI Foundry project with a single command.

Why a .NET file-based App?

I created this tool as a .NET single-file app. This is a standalone .cs file, making it incredibly simple to distribute and use.

Key advantages:

  • No installation required: Simply take the app file .cs and run it with dotnet run
  • Cross-platform: Build versions for Windows (x64), Linux (x64), and macOS (ARM64)
  • Easy deployment: Copy a single file to deploy the application
  • Shebang support: On Linux/macOS, the compiled binary can be made executable directly with chmod +x and run like a native script

This makes it perfect for developer tools, allowing you to keep the executable in your PATH and use it from anywhere.

Prerequisites

Before using this tool, you’ll need:

  1. Azure AI Foundry Project: An Azure AI Foundry project with agents to manage
  2. Azure CLI: Authenticated via az login (the tool uses your CLI credentials)
  3. .NET 10 SDK: Only required if building from source (not needed if using pre-built binaries)

The Script

The script uses the following NuGet packages:

  • Azure.AI.Agents.Persistent (1.2.0-beta.8): The official SDK for managing persistent agents in Azure AI Foundry
  • Azure.Identity (1.18.0-beta.2): For authentication using DefaultAzureCredential

The script uses simple command-line argument parsing (no external CLI framework needed), making it lightweight and easy to understand.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/local/share/dotnet/dotnet run

#:package Azure.AI.Agents.Persistent@1.2.0-beta.8
#:package Azure.Identity@1.18.0-beta.2

using System;
using Azure.AI.Agents.Persistent;
using Azure.Identity;

// Parse command line arguments
if (args.Length == 0)
{
    Console.WriteLine("Usage:");
    Console.WriteLine("  agents list              - List all agents");
    Console.WriteLine("  agents delete [--yes]    - Delete all agents");
    return 1;
}

var command = args[0].ToLower();

switch (command)
{
    case "list":
        await ListAgentsAsync();
        break;
    
    case "delete":
        var skipConfirmation = args.Length > 1 && args[1] == "--yes";
        await DeleteAgentsAsync(skipConfirmation);
        break;
    
    default:
        Console.WriteLine($"Unknown command: {command}");
        Console.WriteLine("Valid commands: list, delete");
        return 1;
}

return 0;

// ---------- helper methods ----------
async Task ListAgentsAsync()
{
    var client = CreateClient();
    
    await foreach (var agent in client.Administration.GetAgentsAsync())
    {
        Console.WriteLine($"{agent.Id}\t{agent.Name}");
    }
}

async Task DeleteAgentsAsync(bool skipConfirmation)
{
    var client = CreateClient();

    // enumerate first (pagination-safe)
    var ids = new List<string>();
    await foreach (var agent in client.Administration.GetAgentsAsync())
        ids.Add(agent.Id);

    if (ids.Count == 0)
    {
        Console.WriteLine("No agents found.");
        return;
    }

    if (!skipConfirmation)
    {
        Console.Write($"Delete {ids.Count} agents? (yes): ");
        if (Console.ReadLine() != "yes")
            return;
    }

    foreach (var id in ids)
    {
        Console.WriteLine($"Deleting {id}");
        await client.Administration.DeleteAgentAsync(id);
    }
}

static PersistentAgentsClient CreateClient()
{
    var endpoint = Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT")!;
    return new PersistentAgentsClient(endpoint, new DefaultAzureCredential());
}

Download this example on PlayGoKids repository.

How It Works

The script provides two commands:

list - Lists all agents in your Foundry project:

  • Connects to Azure AI Foundry using the PersistentAgentsClient
  • Iterates through all agents using GetAgentsAsync() (handles pagination automatically)
  • Displays each agent’s ID and name in tab-separated format

delete - Deletes all agents:

  • First enumerates all agents into a list (pagination-safe approach)
  • Shows count and prompts for confirmation (unless --yes flag is used)
  • Iterates through each agent and calls DeleteAgentAsync()
  • Provides progress feedback as each agent is deleted

Authentication

The tool uses DefaultAzureCredential from Azure.Identity, which automatically detects the best authentication method:

  1. Local development: Uses credentials from Azure CLI (az login), Azure PowerShell, or Visual Studio
  2. Azure-hosted: Automatically uses Managed Identity when deployed to Azure services
  3. Environment variables: Can use service principal credentials if configured

This means the same code works seamlessly in both local development and production environments without modification.

Usage

First, set your Azure AI Foundry endpoint as an environment variable. You can find this in the Azure portal under your AI Foundry project settings:

1
2
3
4
5
# Windows PowerShell
$env:AZURE_AI_ENDPOINT = "https://your-project.services.ai.azure.com/api/projects/your-project-id"

# Linux/macOS
export AZURE_AI_ENDPOINT="https://your-project.services.ai.azure.com/api/projects/your-project-id"

Make sure you’re authenticated with Azure CLI:

1
az login

Then run the tool on Linux/macOS:

1
2
3
4
5
6
7
8
# List all agents
./agents list

# Delete all agents (with confirmation prompt)
./agents delete

# Delete all agents without confirmation
./agents delete --yes

Run with dotnet run

If you want to run it directly with dotnet run, you can do so as follows:

1
2
# Run it with dotnet run
dotnet run agents.cs list

Conclusion

This simple tool saved me countless hours during development when I needed to quickly clean up test agents between iterations. The combination of .NET’s file-based app and Azure’s DefaultAzureCredential makes it trivial to integrate seamlessly with Azure services.

The full source code is available as a single C# file that you can customize for your needs. Whether you’re managing agents during development or need to clean up after automated tests, having a command-line tool makes the process much more efficient than clicking through the portal.

Comments & Discussion

Join the conversation! Share your thoughts and connect with other readers.