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?

7 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
2 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']]

4 Likes