Skip to main content
Deletes a collection and all the documents it contains. This operation cannot be undone.
This operation is irreversible and will permanently delete all data in the collection. Make sure you have a backup if needed before proceeding.

Method Signature

collections().delete(name: string): Promise<void>

Parameters

name
string
required
The name of the collection to delete.

Returns

This method returns void and does not produce a return value. If the operation succeeds, the collection and all its data are permanently deleted.

Examples

Delete a Collection

import { Client } from "topk-js";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

await client.collections().delete("books");
console.log("Collection deleted successfully");

Delete with Confirmation

import { Client } from "topk-js";
import * as readline from "readline";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

async function deleteWithConfirmation(collectionName) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  return new Promise((resolve) => {
    rl.question(
      `Are you sure you want to delete '${collectionName}'? (yes/no): `,
      async (answer) => {
        rl.close();
        
        if (answer.toLowerCase() === "yes") {
          await client.collections().delete(collectionName);
          console.log("Collection deleted successfully");
          resolve(true);
        } else {
          console.log("Deletion cancelled");
          resolve(false);
        }
      }
    );
  });
}

await deleteWithConfirmation("books");

Delete Multiple Collections

import { Client } from "topk-js";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

const collectionsToDelete = ["temp_collection_1", "temp_collection_2", "test_collection"];

for (const name of collectionsToDelete) {
  try {
    await client.collections().delete(name);
    console.log(`Deleted: ${name}`);
  } catch (error) {
    console.error(`Failed to delete ${name}: ${error.message}`);
  }
}

Safe Delete with Existence Check

import { Client } from "topk-js";

const client = new Client({
  apiKey: process.env.TOPK_API_KEY,
  region: "us-east-1"
});

async function safeDelete(collectionName) {
  try {
    // Check if collection exists first
    await client.collections().get(collectionName);
    
    // Delete if it exists
    await client.collections().delete(collectionName);
    console.log(`Deleted collection: ${collectionName}`);
    return true;
  } catch (error) {
    if (error.message.includes("not found")) {
      console.log(`Collection '${collectionName}' does not exist`);
      return false;
    }
    throw error;
  }
}

await safeDelete("books");
Data Loss Warning: Once a collection is deleted, all documents, indexes, and metadata are permanently removed. This operation cannot be reversed. Always ensure you have backups of critical data before deleting a collection.
If the collection does not exist, this method may throw an error depending on the implementation. Consider using error handling to manage this case.

Build docs developers (and LLMs) love