• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
    • Adding API Routes
    • Change Authentication strategy
    • Fetching the signed in User
    • Reading a Document
    • Creating a Document
    • Configuring Plans
    • Project Configuration
    • Updating the Navigation menu
    • Adding a new translation string
    • Guarding an API Route
    • Adding Pages
    • Updating the Sidebar menu
    • Require Email Verification
    • Fetching the selected Organization
    • Reading a list of Documents
    • Updating a Document
    • Running the Stripe Webhook locally
    • Branding
    • Setting a Default Language
    • Dark Theme
    • Theming
    • Calling API Routes from the client
    • Deleting a Document
    • Updating the Logo
    • Adding a new language in the Next.js Firebase SaaS Kit
    • Checking CSRF Tokens
    • Passing data from server to client
    • Updating the Fonts
    • Adding Pages
    • Guarding Pages
    • Using Lemon Squeezy instead of Stripe
    • Updating the Favicons
    • Using the Language Switcher
    • Environment variables
    • Detect current Locale
    • Setting up Emails

Deleting a Document

Learn how to delete a document in Firestore in your Next.js Firebase app

Deleting a document in Firestore can be achieved using both the Web (Client) and the Admin SDK (Server). Depending on the use-case, you will use one or the other.

  1. Client SDK: When you want to update a document from the browser, you will use the Client SDK. This is the most common use-case, and can be done when the user has the permissions required to delete a document based on their security rules.
  2. Admin SDK: When you want to update a document from a server, you will use the Admin SDK. This is useful when you want to delete a document from a server, or when you want to delete a document that the user does not have permission to delete.

Client SDK

To delete a document, we will use the deleteDoc function from the Web Firebase SDK.

We can use hooks to make sure we have access to the Firestore instance, and the collection we want to delete the document from.

tsx
import { useFirestore } from 'reactfire';
import { useCallback } from 'react';
import { deleteDoc, doc } from "firebase/firestore";
function useDeleteTask() {
const firestore = useFirestore();
return useCallback(
(taskId: string) => {
const tasksDoc = doc(firestore, `/tasks`, taskId);
return deleteDoc(tasksDoc);
},
[firestore]
);
}

We can then use this hook in our component to create a new task.

tsx
import { useCallback } from "react";
const DeleteTask = ({ id }: { id: string }) => {
const deleteTask = useDeleteTask(id);
// ... other code
const onSubmit = useCallback(async (taskId: string) => {
await deleteTask(taskId);
}, [deleteTask]);
// ... other code
};

Admin SDK

To delete a Firestore document, we will use the delete function from the Admin Firebase SDK.

We can use the Admin SDK only in API Route handlers and the getServerSideProps function. For two reasons:

  1. The Admin SDK is for Node.js environments, and cannot be used in the browser.
  2. The Admin SDK requires a service account, which should not be exposed to the browser.
tsx
import getRestFirestore from '~/core/firebase/admin/get-rest-firestore';
async function deleteTaskFromServer(task: {
id: string;
}) {
const firestore = getRestFirestore();
const taskRef = firestore.collection(firestore, `tasks`).doc(task.id);
await taskRef.delete();
return task;
}

You can now use the function deleteTaskFromServer in your API Route handlers and getServerSideProps functions.

On this page
  1. Client SDK
    1. Admin SDK