Skip to content
Storage

删除对象

Learn about deleting objects

当你从桶中删除一个或多个对象时,这些文件会被永久移除,无法恢复。你可以一次删除一个对象,也可以同时删除多个对象。

🌐 When you delete one or more objects from a bucket, the files are permanently removed and not recoverable. You can delete a single object or multiple objects at once.

删除对象 #

🌐 Delete objects

要删除一个或多个对象,使用 remove 方法。

🌐 To delete one or more objects, use the remove method.

1
import { createClient } from '@supabase/supabase-js'
2
const supabase = createClient('your_project_url', 'your_supabase_api_key')
3
4
// ---cut---
5
await supabase.storage.from('bucket').remove(['object-path-2', 'folder/avatar2.png'])

清空大桶 #

🌐 Emptying large buckets

通过 Supabase 仪表板或存储 API 删除对象,每个存储桶有 200,000 个对象的硬性限制。如果一个存储桶中的对象超过 200,000 个,清空存储桶的操作就会失败。

🌐 Deleting objects via the Supabase Dashboard or Storage API has a hard limit of 200,000 objects per bucket. If a bucket contains more than 200,000 objects, the empty bucket operation will fail.

清空大桶的推荐方法是使用支持 Supabase S3 协议的 AWS CLI。你可以通过使用 sync 命令将你的桶与一个空的本地目录同步,从而删除所有对象,这样做可以安全地删除所有对象。

🌐 The recommended approach for emptying large buckets is to use the AWS CLI with Supabase's S3 protocol support. You can delete all objects by using the sync command to sync your bucket with an empty local directory, which will safely delete all objects.

  1. 安装 AWS CLI:根据你的操作系统,按照AWS CLI 安装指南进行操作。
  2. 在 Supabase 中设置 S3 凭证:按照 Supabase S3 身份验证指南 生成你的 S3 访问凭证。
  3. 配置 AWS 配置文件:运行以下命令,并粘贴你在 Supabase 中创建的凭据。配置文件名称 supabase-s3 可以是你想要的任何名称,只要它与你在同步命令中使用的值匹配即可。
1
aws configure --profile supabase-s3
  1. 创建一个空目录并将其同步到你的存储桶
1
# Create a local empty directory
2
mkdir empty-dir
3
4
# Sync the empty directory to your bucket with --delete enabled
5
# This will delete all objects in the bucket
6
aws s3 sync empty-dir/ s3://your-bucket-name --delete --profile supabase-s3 --endpoint-url https://<project-ref>.supabase.co/storage/v1/s3 --region <your-region>

your-bucket-name<project-ref><your-region> 替换成你实际的值。对于大型存储桶,这个操作可能需要一些时间,但不会留下任何孤立的对象。

🌐 Replace your-bucket-name, <project-ref>, and <your-region> with your actual values. This operation may take a while for large buckets, but it will not leave any orphaned objects behind.

RLS#

要删除一个对象,用户必须对该对象拥有 delete 权限。例如:

🌐 To delete an object, the user must have the delete permission on the object. For example:

1
create policy "User can delete their own objects"
2
on storage.objects
3
for delete
4
TO authenticated
5
USING (
6
owner = (select auth.uid()::text)
7
);