Adding Delete Note Functionality To Cloudnotebook App

Posted on October 31, 2026 by Vishesh Namdev
Python C C++ Javascript React JS
Adding Delete Note Functionality to CloudNotebook App

Adding Delete Note Functionality to CloudNotebook App ๐Ÿ—‘๏ธ๐Ÿ“ A notes app isn't really useful if users can only add notes and never remove the ones they don't need anymore. In this part of our CloudNotebook series, we'll add a working Delete button to every note card, wire it up to our backend's DELETE API, and instantly update the UI โ€” no page refresh required.

In this tutorial, we will learn:

  • How delete operations work in a full-stack MERN notes app
  • Adding a deleteNote function inside NoteState.js (Context API)
  • Calling the backend's DELETE /api/notes/deletenote/:id route
  • Removing a note from the UI instantly using Array.filter()
  • Adding a delete icon/button to NoteItem.js
  • Showing a success alert after a note is deleted
  • ---

    Why Delete Note Functionality Matters

    Every note-taking app โ€” Google Keep, Notion, Evernote โ€” lets users clean up their workspace by deleting notes they no longer need. For CloudNotebook, adding delete functionality means two things have to happen together: the note must be removed from the database (via our backend API), and it must disappear from the UI immediately, without the user having to refresh the page.

    ---

    Step 1: Add a deleteNote Function in NoteState.js

    Open context/notes/NoteState.js โ€” this is where all our note-related logic (add, edit, fetch) already lives using React's Context API. Let's add a deleteNote function here.

    import React, { useState } from "react";
    import NoteContext from "./noteContext";
     
    const NoteState = (props) => {
      const host = "http://localhost:5000";
      const [notes, setNotes] = useState([]);
     
      // Delete a note by its id
      const deleteNote = async (id) => {
        const response = await fetch(`${host}/api/notes/deletenote/${id}`, {
          method: "DELETE",
          headers: {
            "Content-Type": "application/json",
            "auth-token": localStorage.getItem("token"),
          },
        });
     
        const json = response.json();
        console.log(json);
     
        // Remove the deleted note from local state
        const newNotes = notes.filter((note) => note._id !== id);
        setNotes(newNotes);
      };
     
      return (
        <NoteContext.Provider value={{ notes, setNotes, deleteNote }}>
          {props.children}
        </NoteContext.Provider>
      );
    };
     
    export default NoteState;
    ---

    Step 2: Add a Delete Icon to NoteItem.js

    Now open components/NoteItem.js โ€” this is the card component that renders each individual note. Let's import useContext, grab deleteNote from our context, and attach it to a delete icon.

    import React, { useContext } from "react";
    import NoteContext from "../context/notes/noteContext";
     
    const NoteItem = (props) => {
      const context = useContext(NoteContext);
      const { deleteNote } = context;
      const { note } = props;
     
      return (
        <div className="col-md-3">
          <div className="card my-3">
            <div className="card-body">
              <div className="d-flex align-items-center">
                <h5 className="card-title mb-0 flex-grow-1">{note.title}</h5>
                <i
                  className="fa-solid fa-trash mx-2"
                  style={{ cursor: "pointer" }}
                  onClick={() => { deleteNote(note._id) }}
                ></i>
              </div>
              <p className="card-text">{note.description}</p>
            </div>
          </div>
        </div>
      );
    };
     
    export default NoteItem;
    ---

    Step 3: Confirm Before Deleting (Optional but Recommended)

    Accidentally deleting an important note is frustrating. Let's add a simple confirmation using window.confirm before calling deleteNote.

    <i
      className="fa-solid fa-trash mx-2"
      style={{ cursor: "pointer" }}
      onClick={() => {
        const confirmDelete = window.confirm("Are you sure you want to delete this note?");
        if (confirmDelete) {
          deleteNote(note._id);
        }
      }}
    ></i>
    ---

    Step 4: Show a Success Alert After Deletion

    Let's give the user quick feedback using our existing Alert component (already used for add/edit notes) so they know the deletion actually worked.

    const NoteItem = (props) => {
      const context = useContext(NoteContext);
      const { deleteNote } = context;
      const { note, showAlert } = props;
     
      const handleDelete = () => {
        const confirmDelete = window.confirm("Are you sure you want to delete this note?");
        if (confirmDelete) {
          deleteNote(note._id);
          showAlert("Note deleted successfully", "success");
        }
      };
     
      return (
        <div className="col-md-3">
          <div className="card my-3">
            <div className="card-body">
              <div className="d-flex align-items-center">
                <h5 className="card-title mb-0 flex-grow-1">{note.title}</h5>
                <i
                  className="fa-solid fa-trash mx-2"
                  style={{ cursor: "pointer" }}
                  onClick={handleDelete}
                ></i>
              </div>
              <p className="card-text">{note.description}</p>
            </div>
          </div>
        </div>
      );
    };
     
    export default NoteItem;
    ---

    Step 5: Full Updated NoteItem.js

    Here is the complete NoteItem.js file with the delete icon, confirmation, and success alert all wired together.

    import React, { useContext } from "react";
    import NoteContext from "../context/notes/noteContext";
     
    const NoteItem = (props) => {
      const context = useContext(NoteContext);
      const { deleteNote } = context;
      const { note, showAlert } = props;
     
      const handleDelete = () => {
        const confirmDelete = window.confirm("Are you sure you want to delete this note?");
        if (confirmDelete) {
          deleteNote(note._id);
          showAlert("Note deleted successfully", "success");
        }
      };
     
      return (
        <div className="col-md-3">
          <div className="card my-3">
            <div className="card-body">
              <div className="d-flex align-items-center">
                <h5 className="card-title mb-0 flex-grow-1">{note.title}</h5>
                <i
                  className="fa-solid fa-trash mx-2"
                  style={{ cursor: "pointer" }}
                  onClick={handleDelete}
                ></i>
              </div>
              <p className="card-text">{note.description}</p>
            </div>
          </div>
        </div>
      );
    };
     
    export default NoteItem;
    ---

    How Delete Note Functionality Works

    Part What It Does Where It Lives
    deleteNote(id) Sends a DELETE request to the backend and removes the note from local state context/notes/NoteState.js
    Trash icon Triggers the delete action when a user clicks it on a specific note card components/NoteItem.js
    window.confirm Prevents accidental deletions by asking the user to confirm first components/NoteItem.js
    Array.filter() Instantly updates the notes list in the UI without needing a page refresh context/notes/NoteState.js
    ---

    Frequently Asked Questions

    Q: Why use Array.filter() instead of refetching all notes after delete?
    Refetching every note after a single delete means an extra, unnecessary API call. Filtering the existing notes array by excluding the deleted _id updates the UI instantly and keeps the app feeling fast.

    Q: Do I need authentication to delete a note in CloudNotebook?
    Yes โ€” the DELETE request includes an auth-token header so the backend can verify that the logged-in user actually owns the note before deleting it.

    Q: What happens if the delete API call fails?
    In production, you should wrap the fetch call in a try/catch and only call setNotes after confirming a successful response, so the UI never shows a note as deleted when it still exists in the database.

    ---

    Features and Learnings:-

  • Understood how delete operations work across both the UI and the backend in a MERN app.
  • Added a deleteNote function inside the Context API (NoteState.js).
  • Called the backend's DELETE /api/notes/deletenote/:id route with an auth token.
  • Updated the notes list instantly in the UI using Array.filter().
  • Added a clickable trash icon to each note card in NoteItem.js.
  • Prevented accidental deletions with a window.confirm dialog.
  • Displayed a success alert after a note is deleted.
  • Prepared the app for the next step: adding Edit Note functionality.
  • ๐Ÿ“ข Important Note ๐Ÿ“ข

    How did you feel about this post?

    ๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

    Was this helpful?

    ๐Ÿ‘ ๐Ÿ‘Ž