A command for Deleting all (selected) files from the API

The way of deleting files in the api is hard if you uploaded a lot of files in an assistant or something else, and want to delete a lot of them. The series of commands down bellow was the way I found to delete all files from the API, of course you can select which files will stay, by deleting them from the api_files.json before running Command 2, this is the (selected) part in the title. (The commands where run in powershell)

Command 1 - creates the api_files.json file(replace DOT for . and DSLASH for //):
curl https:DSLASHapiDOTopenaiDOTcom/v1/files \ -H “Authorization: Bearer $OPENAI_API_KEY”>api_files.json

Command 2 - delete all files listed in api_files.json(replace DOT for . and DSLASH for //):
$jsonContent = Get-Content -Raw -Path ‘path\to\your\api_files.json’ | ConvertFrom-Json

foreach ($entry in $jsonContent.data) {
$idValue = $entry.id
$curlCommand = “curl https:DSLASHapiDOTopenaiDOTcom/v1/files/$idValue -X DELETE -H ‘Authorization: Bearer $OPENAI_API_KEY’”
Invoke-Expression $curlCommand
}

3 Likes

Thanks for sharing.

Hope you stick around. We’ve got a great AI Dev community garden growing here, and it sounds like you’ll fit right in.