Deleting records from a Supabase database

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I'm sure you saw this article coming after we just learned how to insert records in a Supabase database.
Let's say we accidentally added the wrong country that we want to remove...
How do we go about that?
The first thing we want to add is a delete button so we have something to click on.
We'll use a button with the raw svg from a Fontawesome icon.
<button onClick={() => deleteCountry(country.id)}>
<svg
xmlns='http://www.w3.org/2000/svg'
aria-hidden='true'
focusable='false'
role='img'
viewBox='0 0 448 512'
width='0.75rem'
>
<path
fill='currentColor'
d='M32 464C32 490.5 53.5 512 80 512h288c26.5 0 48-21.5 48-48V128H32V464zM304 208C304 199.1 311.1 192 320 192s16 7.125 16 16v224c0 8.875-7.125 16-16 16s-16-7.125-16-16V208zM208 208C208 199.1 215.1 192 224 192s16 7.125 16 16v224c0 8.875-7.125 16-16 16s-16-7.125-16-16V208zM112 208C112 199.1 119.1 192 128 192s16 7.125 16 16v224C144 440.9 136.9 448 128 448s-16-7.125-16-16V208zM432 32H320l-11.58-23.16c-2.709-5.42-8.25-8.844-14.31-8.844H153.9c-6.061 0-11.6 3.424-14.31 8.844L128 32H16c-8.836 0-16 7.162-16 16V80c0 8.836 7.164 16 16 16h416c8.838 0 16-7.164 16-16V48C448 39.16 440.8 32 432 32z'
/>
</svg>
</button>
You might have spotted the deleteCountry function above. We pass the country id to this function.
So let's go ahead and create the function.
This delete country function is super easy, as we can leverage our Supabase setup and simply execute a delete query.
const deleteCountry = async (countryId) => {
try {
await supabase.from('countries').delete().eq('id', countryId);
setCountries(countries.filter((country) => country.id != countryId));
} catch (error) {
console.log('error', error);
}
};
Here you can see the delete query is as simple as calling the delete() method on a row that equals this id.
Then we filter the country from the existing list of countries we are showing to the user.
And all this will result in the following action:

I hope you enjoyed this article. I've uploaded everything to GitHub if you wish to look at your own pace.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter