How to remove existing records from Notion database before adding new data via API

I’m working with a Notion workspace where I track app statistics from GitHub and other platforms. I’ve set up a database to store download metrics using Notion’s API.

Currently, my script successfully appends new records to the database, but I’m struggling with clearing old entries or updating existing ones before adding fresh data. I need to either wipe the database clean or modify existing records instead of just adding more.

Here’s my current implementation:

import { Client } from "@notionhq/client";
import dotenv from "dotenv";
import { Octokit } from "@octokit/rest";

dotenv.config();

const githubAPI = new Octokit();
const notionClient = new Client({ auth: process.env.NOTION_TOKEN });
const dbId = process.env.NOTION_DATABASE_ID;

async function createRecord(version, fileName, downloads, platform) {
  try {
    await notionClient.request({
      path: "pages",
      method: "POST",
      body: {
        parent: { database_id: dbId },
        properties: {
          Release: {
            title: [{
              text: { content: version }
            }]
          },
          Filename: {
            rich_text: [{
              text: { content: fileName }
            }]
          },
          Downloads: {
            type: "number",
            number: downloads
          },
          Categories: {
            multi_select: [{ name: "GitHub" }, { name: platform }]
          }
        }
      }
    });
    console.log("Record created successfully");
  } catch (err) {
    console.error(err.body);
  }
}

(async () => {
  const repoReleases = await githubAPI.repos.listReleases({
    owner: "MyUsername",
    repo: "my-project"
  });
  
  const releaseData = repoReleases.data;
  
  for (let item of releaseData) {
    for (let file of item.assets) {
      const osType = file.name.includes("mac") ? "macOS" : "Windows";
      await createRecord(
        item.tag_name,
        file.name,
        file.download_count,
        osType
      );
    }
  }
})();

How can I modify this to clear existing database entries before populating it with updated information?

I’ve hit this exact issue building automated reporting tools. Here’s what works: query your database for all records, delete them, then insert the new ones. You’ll query the database endpoint to get all pages, then loop through and delete each one. Add this to your script: async function clearDatabase() { const response = await notionClient.databases.query({ database_id: dbId }); for (const page of response.results) { await notionClient.pages.update({ page_id: page.id, archived: true }); } } Run clearDatabase() before your existing loop that creates records. Heads up - Notion doesn’t actually delete pages, just archives them. This approach has been rock solid for me across multiple projects and handles pagination automatically if you’ve got tons of records.

Skip clearing everything first. Instead, check if records already exist and update them rather than archiving. I query the database with filters to find existing records that match my criteria, then update those instead of creating duplicates. Filter by the Release property to check if a version already exists. If it does, use the pages.update endpoint to modify the existing record. If not, create a new one. This keeps your database from getting cluttered with archived entries. The key is using the filter parameter in your database query to search for specific release versions before deciding whether to update or create. Way more efficient than mass deletion and keeps things cleaner long-term.

Honestly, i just archive everything upfront then recreate it all. Not the most efficient approach, but it works for most cases and you dont need to mess with matching logic. Run a database query without filters to grab all pages, archive them, then run your creation loop. Takes maybe 30 seconds extra but saves you from debugging messy update/create logic.