List and delete all threads

The assistants API allow you to create, retrieve, modify, and delete threads. But there seems to be no way to list or delete all threads.

This means that if I programmatically create a bunch of threads (whether or not I add messages to them), those threads will presumably be idle somewhere attached to the assistant.

It would be nice to have an way to list and/or delete all threads for the sake of housekeeping. Or does this not matter?

8 Likes

Yeah. It actually freaked me out when I first tested it a couple of times. I was not able to delete a couple of threads. I was afraid it will go on and on and accumulate charges. lol

1 Like
3 Likes

Using the python requests library and the session token from above, this looks like…

import requests as req

url = "https://api.openai.com/v1/threads"
headers = {
    "Authorization": f"Bearer {token}", 
    "Openai-Organization": f"{org}", 
    "OpenAI-Beta": "assistants=v1"
}

resp = req.get(url, headers=headers)

A full script to delete all threads:

import requests as req
from alive_progress import alive_it

token = "the_token"
org = "myorg"
url = "https://api.openai.com/v1/threads"

headers = {
    "Authorization": f"Bearer {token}", 
    "Openai-Organization": f"{org}", 
    "OpenAI-Beta": "assistants=v1"
}
params = {"limit": 10}
resp = req.get(url, headers=headers, params=params)
ids = [t['id'] for t in resp.json()['data']]

while len(ids) > 0:
    for tid in alive_it(ids, force_tty=True):
        client.beta.threads.delete(tid)
        time.sleep(1)
    resp = req.get(url, headers=headers, params=params)
    ids = [t['id'] for t in resp.json()['data']]

6 Likes

This still works however I found that I had to add my project key as well. You can find it in your profile settings and in the inspector.

I expanded on this and posted python code a while back, giving an “organization explorer”, using web’s oauth mechanism instead to get account information and session key, then letting you list all organizations and projects within them, then a demo of listing that project’s threads (or the playground’s threads) where you could then continue writing the actual deleting.