How to delete an OpenAI assistant
Quick answer
To delete an OpenAI assistant, use the
client.assistants.delete() method from the OpenAI Python SDK v1, passing the assistant's unique ID. This requires initializing OpenAI with your API key and specifying the correct assistant ID.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the latest OpenAI Python SDK and set your API key as an environment variable.
- Run
pip install openai>=1.0to install the SDK. - Set your API key in your environment:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows).
pip install openai>=1.0 Step by step
Use the following Python code to delete an OpenAI assistant by its ID. Replace assistant_id with the actual assistant identifier.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
assistant_id = "your-assistant-id"
response = client.assistants.delete(id=assistant_id)
print("Deleted assistant:", response) output
Deleted assistant: {'id': 'your-assistant-id', 'deleted': True} Common variations
You can delete assistants asynchronously using asyncio with the OpenAI SDK or delete multiple assistants in a loop. The model parameter is not required for deletion.
import os
import asyncio
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def delete_assistant(assistant_id: str):
response = await client.assistants.delete(id=assistant_id)
print(f"Deleted assistant {assistant_id}:", response)
async def main():
assistant_ids = ["assistant-id-1", "assistant-id-2"]
await asyncio.gather(*(delete_assistant(aid) for aid in assistant_ids))
asyncio.run(main()) output
Deleted assistant assistant-id-1: {'id': 'assistant-id-1', 'deleted': True}
Deleted assistant assistant-id-2: {'id': 'assistant-id-2', 'deleted': True} Troubleshooting
- If you get a
404 Not Founderror, verify theassistant_idis correct and the assistant exists. - If you see
401 Unauthorized, check your API key is set correctly inOPENAI_API_KEY. - Ensure you have permission to delete the assistant; some assistants may be protected or require specific scopes.
Key Takeaways
- Use
client.assistants.delete(id=assistant_id)to delete an OpenAI assistant. - Always set your API key securely via environment variables.
- Check for correct assistant ID and permissions to avoid errors.