/supabase-tutorials

How to delete files from Supabase storage?

Learn how to delete files from Supabase Storage with this step-by-step guide on setting up the client, listing files, and safely removing them.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to delete files from Supabase storage?

 

Step 1: Set Up Supabase Client in Your Project

 

To interact with Supabase storage, you first need to set up the Supabase client in your application. If you haven’t already set up a Supabase project, follow these steps:

Create a Supabase account, and create a new project there. Once the project is created, you'll find the API keys on the project’s settings under the "API" tab. These will be used to set up the client.

Next, set up your project. If you're using Node.js, you can install the Supabase client library using npm:


npm install @supabase/supabase-js

After installing, set up the Supabase client in your code:


// Import the Supabase client
import { createClient } from '@supabase/supabase-js'

// Use your Supabase URL and API Key from your project by replacing the placeholders below
const supabaseUrl = 'https://your-supabase-url.supabase.co'
const supabaseKey = 'your-anon-key'

// Create the Supabase client
const supabase = createClient(supabaseUrl, supabaseKey)

 

Step 2: Access the Storage for Deleting Files

 

With the Supabase client set up, you can now proceed to delete files from your Supabase storage bucket. Assume you have already uploaded some files into a bucket, and you know the file paths of these files.

If you need to list files to find the correct path, you can do the following:


// List files in a storage bucket
async function listFiles() {
  const { data, error } = await supabase.storage.from('your-bucket-name').list('')
  if (error) {
    console.error("Error listing files: ", error.message)
  } else {
    console.log("Files: ", data)
  }
}

// Call the listFiles function
listFiles()

Simply replace 'your-bucket-name' with the actual bucket name you want to access.

 

Step 3: Delete a File from Supabase Storage

 

To delete a file, you need to know the path to that specific file within the storage bucket. Use the following function to delete a file:


// Delete a file
async function deleteFile() {
  const filePath = 'path/to/your/file.ext'
  const { data, error } = await supabase.storage.from('your-bucket-name').remove([filePath])

  if (error) {
    console.error("Error deleting file: ", error.message)
  } else {
    console.log(`File deleted successfully: ${filePath}`)
  }
}

// Call the deleteFile function
deleteFile()

Replace 'path/to/your/file.ext' with the path to the file you want to delete, and 'your-bucket-name' with your actual storage bucket name.

 

Step 4: Handle Possible Errors

 

While performing operations, you should always handle potential errors gracefully. You might want to implement more advanced error handling based on your application's needs.

Consider using try-catch blocks for better control and longer processes:


// Improved file delete function with try-catch
async function improvedDeleteFile() {
  try {
    const filePath = 'path/to/your/file.ext'
    const { data, error } = await supabase.storage.from('your-bucket-name').remove([filePath])

    if (error) throw error

    console.log(`File deleted successfully: ${filePath}`)
  } catch (error) {
    console.error("Unexpected error occurred while deleting file: ", error)
  }
}

// Call the improved file delete function
improvedDeleteFile()

By properly handling errors, you can improve user experience and enhance the reliability of your application.

 

Step 5: Finalize and Test

 

Once everything is set up, you should test your delete functionality to ensure it works as expected. Try deleting a few files, checking the operation’s result, and ensuring that the files are indeed removed from your storage.

By following these steps, you have successfully set up a way to delete files from Supabase storage. Happy coding!

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022