Skip to content

feat(agents): add azd ai agent delete command#8519

Open
v1212 wants to merge 2 commits into
mainfrom
jw/agent-delete
Open

feat(agents): add azd ai agent delete command#8519
v1212 wants to merge 2 commits into
mainfrom
jw/agent-delete

Conversation

@v1212
Copy link
Copy Markdown
Collaborator

@v1212 v1212 commented Jun 2, 2026

Summary

Adds azd ai agent delete [name] command to delete a hosted agent from the Foundry service.

Fixes #8520

CLI Help

Delete a hosted agent and all of its versions.

If the agent has active sessions, deletion will fail unless --force is passed.
Use --force to terminate active sessions and delete the agent.

The agent name is resolved from the azd environment when omitted.

Usage:
  agent delete [name] [flags]

Examples:
  # Delete agent (auto-resolves name from azure.yaml)
  azd ai agent delete

  # Delete a specific agent by name
  azd ai agent delete my-agent

  # Force-delete even if active sessions exist
  azd ai agent delete my-agent --force

Flags:
      --force   Force deletion even if the agent has active sessions
  -h, --help    help for delete

Global Flags:
  -C, --cwd string           Sets the current working directory
      --debug                Enables debug and diagnostics logging
  -e, --environment string   The name of the environment to use
      --no-prompt            Runs without prompts
  -o, --output string        The output format (supported: json, none) (default "none")

Behavior

Scenario Result
azd ai agent delete Auto-resolves agent name from azure.yaml, deletes (fails if active sessions)
azd ai agent delete my-agent Deletes the named agent (fails if active sessions)
azd ai agent delete --force Force-deletes, terminates any active sessions
azd ai agent delete --output json Returns structured JSON response
Agent not found Error: agent not found, suggests azd ai agent show
Active sessions exist (no --force) Error: suggests --force or azd ai agent sessions delete

API Mapping (Vienna)

DELETE /agents/{agentName}?api-version=v1&force={true|false}

Source: AgentController.cs:649 - [FromQuery] bool force = false

Note: No [FoundryFeatures(...)] gate on DeleteAgent (management-plane operation).

Changes

File Change
cmd/delete.go New delete command implementation
cmd/delete_test.go Command structure tests (args, flags)
cmd/root.go Register newDeleteCommand
agent_api/operations.go Add force bool param to DeleteAgent()
agent_api/operations_test.go Tests for force=true/false URL encoding, 404 handling
exterrors/codes.go Add OpDeleteAgent, CodeAgentNotFound, CodeAgentHasActiveSessions

Testing

  • All existing tests pass (no breaking changes to DeleteAgent callers - there were none)
  • New tests verify:
    • Command accepts 0 or 1 positional arg, rejects 2+
    • --force flag registered with default=false
    • DeleteAgent client sends correct force=true / force=false in URL query
    • 404 response returns error

@github-actions github-actions Bot added the ext-agents azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes} extensions label Jun 2, 2026
@v1212 v1212 force-pushed the jw/agent-delete branch 2 times, most recently from b8eb03e to adac334 Compare June 2, 2026 04:09
@v1212 v1212 marked this pull request as ready for review June 2, 2026 04:12
@v1212 v1212 requested a review from JeffreyCA as a code owner June 2, 2026 04:12
Copilot AI review requested due to automatic review settings June 2, 2026 04:12
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

📋 Prioritization Note

Thanks for the contribution! The linked issue isn't in the current milestone yet.
Review may take a bit longer — reach out to @rajeshkamal5050 or @kristenwomack if you'd like to discuss prioritization.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new azd ai agent delete [name] command to the Azure AI Agents extension to delete a hosted agent (optionally force-deleting when active sessions exist), and wires the underlying client operation + error codes needed to support the UX.

Changes:

  • Add agent delete cobra command and action implementation (including --force and --output json/none).
  • Extend AgentClient.DeleteAgent to include a force query parameter and add URL/404 tests.
  • Introduce delete-specific external error codes + operation name and register the new command in the root command tree.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
cli/azd/extensions/azure.ai.agents/internal/pkg/agents/agent_api/operations.go Extends DeleteAgent API call to include force query parameter.
cli/azd/extensions/azure.ai.agents/internal/pkg/agents/agent_api/operations_test.go Adds tests for force=true/false query encoding and 404 behavior.
cli/azd/extensions/azure.ai.agents/internal/exterrors/codes.go Adds delete operation name and delete-specific validation error codes.
cli/azd/extensions/azure.ai.agents/internal/cmd/root.go Registers the new delete command under the extension root.
cli/azd/extensions/azure.ai.agents/internal/cmd/delete.go Implements the new agent delete command, confirmation prompt, error mapping, output formatting, and env-var cleanup.
cli/azd/extensions/azure.ai.agents/internal/cmd/delete_test.go Adds basic command shape tests (args + --force flag presence/default).

Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/delete.go
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/delete.go
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/delete.go Outdated
Implements `azd ai agent delete [name]` to delete a hosted agent via the
Foundry API. Key behaviors:

- Default: force=false (fails if agent has active sessions)
- --force: terminates active sessions and force-deletes
- Confirmation prompt before deletion (skipped with --no-prompt)
- Agent name auto-resolved from azure.yaml / azd environment
- Cleans up AGENT_{KEY}_NAME, _VERSION, _ENDPOINT env vars on success
- Supports --output json for structured output
- Handles 404 (not found) and 409 (active sessions) gracefully

Also updates the DeleteAgent client method in operations.go to accept
a force parameter matching the Vienna API's ?force= query parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@v1212 v1212 force-pushed the jw/agent-delete branch from adac334 to 051202d Compare June 2, 2026 05:28
@v1212
Copy link
Copy Markdown
Collaborator Author

v1212 commented Jun 2, 2026

Addressed Copilot review comments:

Comment 1 (name resolution): This follows the same pattern as \show, \invoke, \monitor, and \session\ commands they all pass the positional arg to
esolveAgentServiceFromProject\ which uses it as the azure.yaml service name to look up the deployed agent name from env vars. This is the established convention in this extension.

Comment 2 (cancellation handling): Fixed now returns \�xterrors.Cancelled()\ instead of printing to stdout. Consistent with the rest of the codebase.

Comment 3 (test coverage): Added error classification tests for 404 \CodeAgentNotFound, 409 \CodeAgentHasActiveSessions\ (verifying suggestion mentions --force), and 500 \ServiceError. Same pattern as session_test.go.

Move error classification logic from inline switch in Run() to a
package-level classifyDeleteError function. Tests call it directly
instead of duplicating the logic.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext-agents azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes} extensions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(agents): add azd ai agent delete command

2 participants