Deleting all threads of an org in batches of 10

This is a simple repost of List and delete all threads - #4 by jkyle

Much thanks to jkyle and sashirestela for the demonstration.

image

  1. Open the developer tools in Chrome and activate the Network tab.
  2. Navigate to https://platform.openai.com/assistants
  3. With the network tab shown, reload the page and capture the requests
  4. Under assistants?limit=10 find the ‘Request Headers’ section > ‘Authorization’
  5. Copy ‘sess-…’ and your organization ID.
import requests as req
from alive_progress import alive_it
from openai import OpenAI
import time

client = OpenAI()

token = 'sess-...'
org = 'org-...'
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']]

This will sequentially delete all your existing threads.

1 Like

Thanks for sharing! I needed to clear out assistants, because I had stupidly created a lot of them during dev and testing. So adapted your code to Joyride (should work in ClojureScript and nbb too):

(ns admin
  (:require ["axios" :as axios]
            ["openai" :as openai]
            [promesa.core :as p]
            [local-config :as conf]))

(defonce ^:private openai (openai/OpenAI.))

(def ^:private headers #js {"Authorization" (str "Bearer " conf/token),
                            "Openai-Organization" conf/org,
                            "OpenaI-Beta" "assistants=v1"})

(defn- purge-assistants-ids! [ids]
  (if (not-empty ids)
    (let [a-id (first ids)]
      (println (count ids) "Deleting: " a-id "\n")
      (p/let [_ (openai.beta.assistants.del a-id)]
        (js/setTimeout #(purge-assistants-ids! (rest ids)) 100)))
    (println "Done")))

(defn purge-assistants! [limit]
  (let [url "https://api.openai.com/v1/assistants"
        params #js {"limit" limit}]
    (p/let [resp (axios/get url #js {:headers headers :params params})
            resp-clj (js->clj resp :keywordize-keys true)
            ids (map #(:id %) (:data (:data resp-clj)))]
      (println "Purge started:" (count ids) "assistants to delete\n")
      (purge-assistants-ids! ids))))

(comment
  (purge-assistants! 20)
  :rcf)