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.

I was able to delete 7 months of threads, took me about 90 minutes. It’s not fun, but if you go into the playground → threads you can click “Load More” to get as many as you want to try to delete, then copy the text by clicking on bottom right corner and dragging all the way up to select all the text. paste in text file. Use GPT, attach file and prompt to extract all lines beginning thread_. Then you just need to script it out to loop through and delete all the ids. I was able to do about 100-200 at a time until the end and it got a little weird and wouldn’t let you load too much, but it worked. It’s the most ridiculous thing that OpenAI one of the leading AI companies, in the year 2024, can’t put a freaking delete option on the playground at miniumum. or a way to select with check boxes in a list and do a batch delete. It makes no sense at all.