Remove the entire content of a folder in storage #4218
-
|
Hello, is it possible to delete the folder or entire content of a folder using storage.remove()? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 23 replies
-
|
No. You can empty a bucket with a call or use the UI. |
Beta Was this translation helpful? Give feedback.
-
|
The UI can also delete complete folders... so do they also iterate through the files and then delete the folder itself? |
Beta Was this translation helpful? Give feedback.
-
|
Problem with the solution from above are folders inside folders. Supabase doesn't provide a way for recursive listing as far as I know so we have to implement it manually, right? |
Beta Was this translation helpful? Give feedback.
-
|
Would be very very nice if it's possible to remove items via a wildcard |
Beta Was this translation helpful? Give feedback.
-
|
I found a workaround that may help. Not an optimal solution but better than the most obvious approach. Consider these two queries: const { data: filePaths, error: filePathsError } = await this.supabase
.schema("storage")
.from("objects")
.select("name")
.eq("bucket_id", 'some-bucket-name')
.like("name", `some-uuid-here%`); // remove or keep the % based on your needs
const { data: listedFiles } = await this.supabase.storage
.from(bucket)
.list(pathWithoutBucket);
console.log({ filePaths, listedFiles });The result: {
"filePaths": [
{
"name": "some-uuid-here/officers/officer-uuid-here/avatar/avatar.png"
}
],
"listedFiles": [
{
"name": "officers",
"id": null,
"updated_at": null,
"created_at": null,
"last_accessed_at": null,
"metadata": null
}
]
}The first query returns the full path of all the objects in the folder Then, you can delete the files using the paths from the first query: const pathsToDelete = filePaths
.filter((path): path is { name: string } => !!path.name)
.map((f) => f.name);
await this.supabase.storage.from(bucket).remove(pathsToDelete);If you're working locally, remember to expose the Side note: This query deletes all files/folders recursively: await this.supabase
.schema('storage')
.from('objects')
.delete()
.eq('bucket', 'some-bucket-name')
.like('name', `some-folder-path%`);...at least, that's what Supabase Studio shows: the files/folders no longer exist after running this query. I chose to go with the first approach because whilst the first approach truly deletes the files/folders, I dunno whether this query simply removes the database records or it actually deletes the files themselves as well. My guess is the former. Maybe a contributor can clarify? Edit — stick with the first approach: |
Beta Was this translation helpful? Give feedback.

No. You can empty a bucket with a call or use the UI.
Otherwise you have to build up a list of file paths from the "folder" and remove them in a loop or as an array. I'm not sure how well that would work for large amount of files in a single call with an array, or looping in a database function. You probably need to run such code in a cloud function or on your server if you have one.
The new studio open source likely has examples of deleting all files in a folder as the user interface does that for you.