删除对象
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.
删除对象应该总是通过 Storage API 来进行,而不是使用 SQL 查询。通过 SQL 查询删除对象不会从存储桶中移除对象,会导致对象成为孤立对象。
🌐 Deleting objects should always be done via the Storage API and NOT via a SQL query. Deleting objects via a SQL query will not remove the object from the bucket and will result in the object being orphaned.
删除对象 #
🌐 Delete objects
要删除一个或多个对象,使用 remove 方法。
🌐 To delete one or more objects, use the remove method.
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient('your_project_url', 'your_supabase_api_key')34// ---cut---5await supabase.storage.from('bucket').remove(['object-path-2', 'folder/avatar2.png'])在删除对象时,使用 remove 方法一次最多只能删除 1000 个对象。
🌐 When deleting objects, there is a limit of 1000 objects at a time using the remove method.
清空大桶 #
🌐 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.
- 安装 AWS CLI:根据你的操作系统,按照AWS CLI 安装指南进行操作。
- 在 Supabase 中设置 S3 凭证:按照 Supabase S3 身份验证指南 生成你的 S3 访问凭证。
- 配置 AWS 配置文件:运行以下命令,并粘贴你在 Supabase 中创建的凭据。配置文件名称
supabase-s3可以是你想要的任何名称,只要它与你在同步命令中使用的值匹配即可。
1aws configure --profile supabase-s3- 创建一个空目录并将其同步到你的存储桶:
1# Create a local empty directory2mkdir empty-dir34# Sync the empty directory to your bucket with --delete enabled5# This will delete all objects in the bucket6aws 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.
--profile 值必须与你在步骤 3 中配置的配置文件名称相匹配。正确的端点 URL 和区域可以在你项目的 存储设置 中找到。
🌐 The --profile value must match the profile name you configured in Step 3. The correct endpoint URL and region can be found in your project's Storage Settings.
RLS#
要删除一个对象,用户必须对该对象拥有 delete 权限。例如:
🌐 To delete an object, the user must have the delete permission on the object. For example:
1create policy "User can delete their own objects"2on storage.objects3for delete4TO authenticated5USING (6 owner = (select auth.uid()::text)7);