Claude - clearHistory()

Clear all chat history

// ==UserScript==
// @name         Claude - clearHistory()
// @description  Clear all chat history
// @version      0.2
// @match        https://claude.ai/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=claude.ai
// @run-at       document-end
// @grant        unsafeWindow
// @grant        GM_cookie
// @namespace https://greasyfork.org/users/1286140
// ==/UserScript==

(() => {
  let org = "";

  GM.cookie.list({ name: "lastActiveOrg" }).then((cookies) =>
    cookies.forEach((cookie) => {
      org = cookie.value;
    }),
  );

  unsafeWindow.clearHistory = () => {
    if (!org) throw Error("Could not retreive current organization id");
    fetch(`https://claude.ai/api/organizations/${org}/chat_conversations`, {
      method: "GET",
      credentials: "include",
    })
      .then((data) => data.json())
      .then((data) =>
        Promise.all(
          data.map((chat) =>
            fetch(
              `https://claude.ai/api/organizations/${org}/chat_conversations/${chat.uuid}`,
              {
                method: "DELETE",
                credentials: "include",
              },
            ),
          ),
        ).then(() => unsafeWindow.location.reload()),
      );
  };

  window.addEventListener("load", () => {
    const root = document.querySelectorAll("h1");
    const button = document.createElement("button");
    button.textContent = "Clear History";
    button.classList.add("block", "mx-auto", "p-4");
    button.addEventListener("click", () => {
      confirm('Clear all chats?') && unsafeWindow.clearHistory();
    });
    root[root.length -1].insertAdjacentElement("afterend", button);
  });
})();