Storage

Delete Objects

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#

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#

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.

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. Install the AWS CLI: Follow the AWS CLI installation guide for your operating system.

  2. Set up S3 credentials in Supabase: Generate your S3 access credentials by following the Supabase S3 authentication guide.

  3. Configure an AWS profile: Run the following command and paste in the credentials you created in Supabase. The profile name supabase-s3 can be anything you want, as long as it matches the value you use in the sync command.

1
aws configure --profile supabase-s3
  1. Create an empty directory and sync it to your bucket:
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>

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#

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
);