Deleting Foundry Agents in Bulk with C#
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
.csand run it withdotnet 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 +xand 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:
- Azure AI Foundry Project: An Azure AI Foundry project with agents to manage
- Azure CLI: Authenticated via
az login(the tool uses your CLI credentials) - .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.
| |
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
--yesflag 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:
- Local development: Uses credentials from Azure CLI (
az login), Azure PowerShell, or Visual Studio - Azure-hosted: Automatically uses Managed Identity when deployed to Azure services
- 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:
| |
Make sure you’re authenticated with Azure CLI:
| |
Then run the tool on Linux/macOS:
| |
Run with dotnet run
If you want to run it directly with dotnet run, you can do so as follows:
| |
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.

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