Delete Supabase Storage Folder with One Simple Function - The Fastest Way

Delete a Supabase storage folder by deleting its content with faster than the Supabase dashboard with one simple function

tech
supabase

Have you ever found yourself impatiently staring at that agonizingly slow progress bar while attempting to delete folders in your Supabase storage? Well, look no further! Today, I am showing you fast and easy method for deleting folders on Supabase storage. Step 1: We all understand the significance of database functions - they are designed for speed and efficiency.

CREATE OR REPLACE FUNCTION bucket_get_all(bucketid text) 
RETURNS SETOF storage.objects
VOLATILE LANGUAGE plpgsql AS $$
BEGIN
    RETURN QUERY
    SELECT *
    FROM storage.objects
    WHERE bucket_id = $1;
END;
$$;

CREATE OR REPLACE FUNCTION bucket_get_all(bucketid text, subpath text) 
RETURNS SETOF storage.objects
VOLATILE LANGUAGE plpgsql AS $$
BEGIN
    RETURN QUERY
    SELECT *
    FROM storage.objects
    WHERE bucket_id = $1 AND name LIKE $2 || '%';
END;
$$;

These two functions query the storage for all file paths, which you can use to delete the folders in bulk, which is way faster than deleting them one by one.

Step 2: Execute the command to delete them.

const folderFiles = (await data.supabase.rpc("bucket_get_all", {bucketid: "BucketName", subpath: "subpath"})).data!
await storage.remove(pathsToDelete)