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 response = await fetch("https://api.ragie.ai/documents", {
    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) {
    if (document.metadata.environment !== "tutorial") {
      continue;
    }

    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 all of the documents in your Ragie instance using the next_cursor parameter for pagination. It collects all document IDs that have metadata with a tutorial environment and then iterates through the pendingDelete list calling the delete document API for each ID.

Note: Using filters with the list API
It's not currently possible to use filters to filter by metadata using the list API but this feature is coming soon!

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.


What’s Next