SupabaseS
Supabase2mo ago
Wes

Cannot delete files from storage bucket

I've looked through previous questions and they all seem to be solved via RLS changes. I've tried every policy I can think of and still cannot delete files from a bucket, either via code or thru the dashboard.


Here is my current policy, this is the only one that exists in the storage section. I'll change it to be more restrictive once I figure out what is stopping me from being able to delete files.

CREATE POLICY "An authenticated user can CRUD any files"
ON storage.objects
FOR ALL
TO authenticated
USING (bucket_id = 'test');


When I try to delete a file via the dashboard, it just hangs with a spinner saying Deleting 1 file(s)...



Here is a sample function that demos the problem I have trying to delete via code, plus a screenshot of the console output. I get a response saying StorageApiError: The related resource does not exist, but I pulled the data straight from a query so I know the file is there and the name is correct.

async function deleteTest({ bucket, folder }) {
    console.log("Running deleteTest for bucket:", bucket, "folder:", folder);

    const { data: listData, error: listError } = await supabase.storage
      .from(bucket)
      .list(folder, {
        limit: 2,
        sortBy: { column: "name", order: "desc" },
      });

    if (listError || !listData) {
      return;
    }

    console.log(`Files in ${bucket} bucket, ${folder} folder:`, listData);

    const fileToDelete = `${folder}/${listData[0].name}`;

    console.log("Deleting file:", fileToDelete);

    const { data: deleteData, error: deleteError } = await supabase.storage
      .from(bucket)
      .remove([fileToDelete]);

    console.log("Delete file error: ", deleteError);
  }
supabase_delete_error_2.png
supabase_delete_error.png
Was this page helpful?