Skip to main content

Truncate Collection

Truncating a collection removes all entities while preserving the collection's schema, constraints, and indexes. It is more efficient than deleting entities because it hides all entities flushed before the current timestamp from searches and queries and drops them in the background.

📘Notes

This feature applies only to managed collections.

Overview

Collection truncation is a high-performance operation that removes all entities from a collection while fully preserving its structural definition, including schema, constraints, and indexes. This keeps the collection immediately ready for new data ingestion without requiring reconfiguration or index rebuilding.

Unlike conventional deletion methods that process records individually and generate extensive transaction logs, truncation operates through an optimized two-step mechanism:

  1. Immediate logical removal

    All entities inserted or deleted before the truncation timestamp are immediately flushed and hidden from searches and queries, making them effectively invisible to subsequent operations.

  2. Efficient physical cleanup

    The system garbage-collects all affected data segments in the background, eliminating the overhead of per-entity deletion processing.

Truncation is ideal for use cases that require rapid, complete dataset resets, such as test environment refreshes, pipeline stage cleanup, or periodic data lifecycle management, where performance and resource efficiency are critical.

Example

The following code examples assume that you already have a collection named my_collection.

python
from pymilvus import MilvusClient, DataType

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)

client.truncate_collection(
collection_name="my_collection"
)
Ctrl I