📝
OpenAIでストレージ内のすべてのファイルを削除する
概要
OpenAIでストレージ内のすべてのファイルを削除する方法に関する備忘録です。
以下のページが参考になりました。
背景
以下のようなコードによってアップロードした複数ファイルを一括削除したいケースがありました。
file_paths = glob("data/txt/*.txt")
file_streams = [open(path, "rb") for path in file_paths]
# Use the upload and poll SDK helper to upload the files, add them to the vector store,
# and poll the status of the file batch for completion.
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)
# You can print the status and the file counts of the batch to see the result of this operation.
print(file_batch.status)
print(file_batch.file_counts)
方法
以下のコードを実行することに、一括削除を行うことができました。
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
を入力することで、一括削除を行うことができました。
== 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
まとめ
とても便利なツールですが、ご利用される際には十分にご注意の上、お使いください。
参考になりましたら幸いです。
Discussion