Step 5: Cleanup (optional)

In this section we will learn about some of Ragie's utility APIs to remove our tutorial documents

In our tutorial we created several documents, but we should remove them so they don't clutter your index.

Here is an example script that we can use to delete all of the created in this tutorial.

const apiKey = "<YOUR API KEY>";

while (true) {
  const url = new URL("https://api.ragie.ai/documents");
  url.searchParams.set("filter", JSON.stringify({ scope: "tutorial" }));

  const response = await fetch(url, {
    headers: { authorization: `Bearer ${apiKey}` },
  });

  if (!response.ok) {
    throw new Error(
      `Failed to retrieve data from Ragie API: ${response.status} ${response.statusText}`
    );
  }
  const payload = await response.json();

  for (const document of payload.documents) {
    const response = await fetch(
      `https://api.ragie.ai/documents/${document.id}`,
      {
        method: "DELETE",
        headers: {
          authorization: `Bearer ${apiKey}`,
        },
      }
    );

    if (!response.ok) {
      throw new Error(
        `Failed to delete document ${document.id}: ${response.status} ${response.statusText}`
      );
    }
    console.log(`Deleted document ${document.id}`);
  }

  if (!payload.pagination.next_cursor) {
    console.warn("No more documents\n");
    break;
  }
}

This script cleans up the documents that we created in this tutorial. It uses the list document API to iterate through a filtered list of the documents in your Ragie instance using the next_cursor parameter for pagination. For each document that is listed, the document is deleted.

There are more utility APIs that help you manage your documents in Ragie. Be sure to check out the API Reference for a complete list of all of the rest APIs that Ragie supports.