Deleting everything in storage

Just a person with OCD (lol) who doesn’t like leftover files. I’m in need of a quick shell script or python to wipe everything in the storage API or whatever the platform uses to store uploaded and generated files. I considered asking GPT but it doesn’t know the updated docs and stuff.

I made a python utility for you for managing assistants files that is a bit safer than a script that only performs catastrophic data loss.

== Assistants file utility ==
[1] Upload file
[2] List all files
[3] List all and delete one of your choice
[4] Delete all assistant files (confirmation required)
[9] Exit
Enter your choice:

Warning: bugs in other people’s code can still lead to assistant-destroying loss, so understand my code first.

Python

from openai import OpenAI
import datetime

# Initialize the OpenAI client
client = OpenAI()

def upload_file():
    filename = input("Enter the filename to upload: ")
    try:
        with open(filename, "rb") as file:
            response = client.files.create(file=file, purpose="assistants")
            print(response)
            print(f"File uploaded successfully: {response.filename} [{response.id}]")
    except FileNotFoundError:
        print("File not found. Please make sure the filename and path are correct.")

def list_files():
    response = client.files.list(purpose="assistants")
    if len(response.data) == 0:
        print("No files found.")
        return
    for file in response.data:
        created_date = datetime.datetime.utcfromtimestamp(file.created_at).strftime('%Y-%m-%d')
        print(f"{file.filename} [{file.id}], Created: {created_date}")

def list_and_delete_file():
    while True:
        response = client.files.list(purpose="assistants")
        files = list(response.data)
        if len(files) == 0:
            print("No files found.")
            return
        for i, file in enumerate(files, start=1):
            created_date = datetime.datetime.utcfromtimestamp(file.created_at).strftime('%Y-%m-%d')
            print(f"[{i}] {file.filename} [{file.id}], Created: {created_date}")
        choice = input("Enter a file number to delete, or any other input to return to menu: ")
        if not choice.isdigit() or int(choice) < 1 or int(choice) > len(files):
            return
        selected_file = files[int(choice) - 1]
        client.files.delete(selected_file.id)
        print(f"File deleted: {selected_file.filename}")

def delete_all_files():
    confirmation = input("This will delete all OpenAI files with purpose 'assistants'.\n Type 'YES' to confirm: ")
    if confirmation == "YES":
        response = client.files.list(purpose="assistants")
        for file in response.data:
            client.files.delete(file.id)
        print("All files with purpose 'assistants' have been deleted.")
    else:
        print("Operation cancelled.")

def main():
    while True:
        print("\n== Assistants file utility ==")
        print("[1] Upload file")
        print("[2] List all files")
        print("[3] List all and delete one of your choice")
        print("[4] Delete all assistant files (confirmation required)")
        print("[9] Exit")
        choice = input("Enter your choice: ")

        if choice == "1":
            upload_file()
        elif choice == "2":
            list_files()
        elif choice == "3":
            list_and_delete_file()
        elif choice == "4":
            delete_all_files()
        elif choice == "9":
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()


4 Likes

Need this also, but new to the forum. Where to add this code? Only in the playground?

The code you see is written in Python, a fourth-generation interpreted scripting language that users of the API are expected to be somewhat familiar with (or at least know it exists). For someone that is completely new to programming, and has no idea what to do with the code, here’s some orientation to the language and how you can set up an execution environment to run such a script (which requires network access, the OpenAI Python library version 1.11 or greater and Python 3.8 - Python 3.11) on various operating systems.

Introduction to Python

(AI completion of my message continues…)

Python is a highly versatile and widely used programming language that appeals to a broad range of users, from beginners to experienced programmers. It’s known for its simplicity and readability, which make it an excellent choice for those who are new to programming. Python’s syntax is clean and straightforward, allowing new developers to grasp programming concepts without getting bogged down by complex syntax rules.

Getting Started with Python

To begin working with Python, you first need to set up a suitable development environment on your computer. This involves installing Python and potentially an Integrated Development Environment (IDE) that will make writing and running your Python code more accessible and more efficient. Python supports multiple operating systems, including Windows, macOS, and Linux, making it a versatile choice for developers.

Installing Python

  1. Download Python: Visit the official Python website (python.org) and navigate to the Downloads section. The site usually offers the latest version for your operating system. Python versions 3.8 to 3.11 are commonly used and supported versions, ensuring compatibility with most libraries and frameworks.

  2. Run the Installer: Once the download is complete, run the installer. For Windows, it’s a .exe file, while macOS and Linux have their respective package formats. During installation, ensure that you tick the option to “Add Python to PATH,” which makes it easier to run Python from the command line.

  3. Verify Installation: Open a command prompt or terminal and type python --version. This command should return the Python version number, indicating that Python is successfully installed on your system.

Setting Up an IDE

While you can write Python code in a simple text editor, using an IDE can significantly enhance your coding experience by providing features like syntax highlighting, code completion, and debugging tools. Some popular IDEs for Python include:

  • IDLE: Included with a Python install, this file editor lets you quickly get started.
  • PyCharm: Developed by JetBrains, PyCharm is a powerful IDE offering a range of features for Python development. It has both a free Community version and a paid Professional version.
  • Visual Studio Code (VS Code): A lightweight, extensible, and free IDE developed by Microsoft. With its vast array of extensions, you can tailor VS Code to your specific needs.
  • Jupyter Notebooks: Particularly popular in data science and academic contexts, Jupyter Notebooks allow you to combine executable Python code with rich text elements, equations, and visualizations.

Running Your First Python Script

With Python and an IDE installed, you’re ready to write and run your first Python script. Here’s a simple example:

  1. Open your IDE and create a new Python file (e.g., hello_world.py).

  2. Type the following code into the file:

    print("Hello, world!")
    
  3. Run the script in your IDE or from the command line by navigating to the script’s directory and typing python hello_world.py. You should see the output “Hello, world!” displayed.

Connecting to External APIs

For scripts that require network access or interactions with external APIs, additional steps may be needed. For instance, to use the OpenAI API, you would need to install the OpenAI Python library and configure your network settings to allow outbound connections.

  1. Install OpenAI Library: Use the Python package manager pip to install the OpenAI library. Open your command line or terminal and type:

    pip install openai
    

    Ensure you have version 1.11 or greater of the library for compatibility with OpenAI library code you’ll see.

  2. Network Access: Make sure your development environment has internet access. If you’re behind a firewall or using a corporate network, you may need to configure proxy settings or obtain permission to access external resources.

  3. Authentication: To use the OpenAI API, you’ll need an API key. Securely store this key and reference it in your Python scripts to authenticate your requests to the OpenAI servers. It will need to be set as an environment variable OPENAI_API_KEY in your OS.

By following these steps, you’ll have set up a functional Python development environment capable of running scripts and connecting to external services like the OpenAI API. Whether you’re developing simple scripts or complex applications, Python’s versatility and the robust ecosystem will provide you with the tools you need to succeed.

OpenAI has their own quickstart guide: https://platform.openai.com/docs/quickstart?context=python

2 Likes

Thank you very much for your prompt and thorough response. I’m looking forward to comparing notes as we go through this new adventure together.

With regard to using your code, I meant “where in the OpenAI platform can I apply this code to my assistant”, as I’m new to the Open AI development experience. I’m actually very familiar with several development languages, including Python. :wink: I’m using Visual Studio 2022 and I’m calling the API using Microsoft’s Azure.AI.OpenAI.Assistants nuget package. Everything seems to work fine, except for deleting and unlinking assistant files.

The part I was missing is nicely covered by the last part of your response. I’m all set - authenticated and connected to the OpenAI environment using VSCode for direct access.

Thanks again.

1 Like

I will respond to this in context but I wanted to know if you had encountered this issue: I used your script and it worked PERFECTLY, it cleared all the existing shared files and allowed even more so a greater control over the existing Assistants. Then they restructured the whole system to be projects, Default Projects, and ones you created. Now, I have an open ticket with OpenAI (nightmare of nightmares) because after changing to that system, all my deleted files and all my deleted assistants magically reappeared in my Default Project sections.

I am not sure whether or not OpenAI is restoring form backups when they make these changes or not or whether those backups fall inside their retention period or whatever, but after the change, EVERY, SINGLE, deleted item that worked from your script has returned. This is in the OpenAI API and not the ChatGPT Team ecosystem which is its own bag or marbles.

Have you been made aware of this and is using your existing script enough given the new tags and parameter labels? Thanks again for your help.

Should work fine - projects are accessed via the API key, so provided you use a project-scoped key (as opposed to the old user-scoped key) there should be no difference.

worked flawlessly, thanks