Delete Assistants via API (or mass delete in front-end)

Hi all,

I can create Assistants via https://api.openai.com/v1/assistants but I dont know how to delete or update assistants. I also cant seem to mass delete them in the front-end. Did anybody work this out?

Best regards,
Martin

1 Like

https://platform.openai.com/docs/api-reference/assistants/deleteAssistant

One could obtain a list of assistants of max size, delete one by one, wait a bit, call another list, continuing to delete.

Providing the API works with your code.

1 Like

This is great, thank you very much.

#!/bin/bash

API_KEY="..."

# List all assistants
echo "Listing all assistants..."
assistants=$(curl -s -H "Authorization: Bearer $API_KEY" \
    -H "Content-Type: application/json" \
    -H "OpenAI-Beta: assistants=v1" \
    "https://api.openai.com/v1/assistants?limit=100")

# Extract IDs of all assistants
ids=$(echo $assistants | jq -r '.data[].id')

# Check if no assistants found
if [ -z "$ids" ]; then
  echo "No assistants to delete."
  exit 0
fi

# Loop through each assistant ID and delete
for id in $ids
do
  echo "Deleting assistant with ID: $id"
  delete_response=$(curl -s -X DELETE -H "Authorization: Bearer $API_KEY" \
      -H "Content-Type: application/json" \
      -H "OpenAI-Beta: assistants=v1" \
      "https://api.openai.com/v1/assistants/$id")
  echo $delete_response
done

echo "All assistants have been deleted."

unset API_KEY

The above script reports “All assistants have been deleted” when assistants over 100 still remain.

Here’s the improved bash script to ensure it continues to list and delete until there are fewer than 100 entities left as a final loop, causing irreparable damage with no user confirmation:

#!/bin/bash

API_KEY="..."  # Replace with your actual API key

# Initialize list length to 100 to start the loop
listlen=100

# Continue looping while list length is greater than or equal to 100
while [ $listlen -ge 100 ]; do
  echo "Listing all assistants..."

  # Fetch the list of assistants (maximum 100 at a time)
  assistants=$(curl -s -H "Authorization: Bearer $API_KEY" \
    -H "Content-Type: application/json" \
    -H "OpenAI-Beta: assistants=v1" \
    "https://api.openai.com/v1/assistants?limit=100")

  # Extract IDs of all assistants
  ids=$(echo "$assistants" | jq -r '.data[].id')

  # Determine the number of assistants returned
  listlen=$(echo "$ids" | wc -l)

  # If no assistants are found, exit the loop
  if [ -z "$ids" ]; then
    echo "No assistants to delete."
    break
  fi

  # Loop through each assistant ID and delete it
  for id in $ids; do
    echo "Deleting assistant with ID: $id"
    delete_response=$(curl -s -X DELETE -H "Authorization: Bearer $API_KEY" \
      -H "Content-Type: application/json" \
      -H "OpenAI-Beta: assistants=v1" \
      "https://api.openai.com/v1/assistants/$id")
    echo $delete_response
  done
done

echo "All assistants have been deleted."

unset API_KEY

(AI added the logic specified, and first wrote everything as function because it is trained stupidly)

1 Like