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
- JavaScript
- Python
collections().delete(name: string): Promise<void>
collections().delete(collection_name: str) -> None
Parameters
The name of the collection to delete.
Returns
This method returnsvoid and does not produce a return value. If the operation succeeds, the collection and all its data are permanently deleted.
Examples
Delete a Collection
- JavaScript
- Python
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");
from topk_sdk import Client
import os
client = Client(
api_key=os.environ["TOPK_API_KEY"],
region="us-east-1"
)
client.collections().delete("books")
print("Collection deleted successfully")
Delete with Confirmation
- JavaScript
- Python
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");
from topk_sdk import Client
import os
client = Client(
api_key=os.environ["TOPK_API_KEY"],
region="us-east-1"
)
def delete_with_confirmation(collection_name: str):
answer = input(
f"Are you sure you want to delete '{collection_name}'? (yes/no): "
)
if answer.lower() == "yes":
client.collections().delete(collection_name)
print("Collection deleted successfully")
return True
else:
print("Deletion cancelled")
return False
delete_with_confirmation("books")
Delete Multiple Collections
- JavaScript
- Python
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}`);
}
}
from topk_sdk import Client
import os
client = Client(
api_key=os.environ["TOPK_API_KEY"],
region="us-east-1"
)
collections_to_delete = ["temp_collection_1", "temp_collection_2", "test_collection"]
for name in collections_to_delete:
try:
client.collections().delete(name)
print(f"Deleted: {name}")
except Exception as error:
print(f"Failed to delete {name}: {error}")
Safe Delete with Existence Check
- JavaScript
- Python
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");
from topk_sdk import Client
import os
client = Client(
api_key=os.environ["TOPK_API_KEY"],
region="us-east-1"
)
def safe_delete(collection_name: str) -> bool:
try:
# Check if collection exists first
client.collections().get(collection_name)
# Delete if it exists
client.collections().delete(collection_name)
print(f"Deleted collection: {collection_name}")
return True
except Exception as error:
if "not found" in str(error):
print(f"Collection '{collection_name}' does not exist")
return False
raise
safe_delete("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.