Clear Tables and Partitions
TRUNCATE clears all data in a specified table or partition in one operation, retaining only the table structure and partition definition. It replaces the active partition data instead of writing row-level delete predicates. Use it when every row in the target table or partition should be removed.
Find a cleanup task
- Choose TRUNCATE: Typical applicable scenarios
- Write a TRUNCATE statement: Command format description
- Run cleanup examples: Common operation demonstrations
- Check cleanup constraints: Restrictions you must know before execution
- Recover retained partitions: How to recover from accidental operations
- Compare TRUNCATE with DELETE: Selection comparison
- Resolve common questions: Frequently asked questions
Choose TRUNCATE
TRUNCATE is suitable for scenarios that require clearing large amounts of data at once:
- Test environment reset: Clear test data in preparation for re-importing.
- Partition data rebuild: Clear specified partitions and then rewrite corrected data.
- Large-scale data cleanup: Avoid evaluating a predicate for every row when clearing an entire target.
- Cleanup after historical data archiving: Clear the original table or partition after archiving is complete.
Write a TRUNCATE statement
Specify the target
TRUNCATE TABLE [db.]tbl [PARTITION(p1, p2, ...)] [FORCE];
Set statement parameters
| Parameter | Required | Description |
|---|---|---|
db. | No | Database name. The current database is used when omitted. |
tbl | Yes | Name of the table to be cleared. |
PARTITION(..) | No | Specifies a list of partition names; only these partitions are cleared. When omitted, the entire table is cleared. |
FORCE | No | Bypasses recycle-bin recovery. Physical storage cleanup is asynchronous. Use with caution. |
Run cleanup examples
Clear an entire table
Clear an entire table
Clear all data in the tbl table under example_db:
TRUNCATE TABLE example_db.tbl;
Clear specified partitions
Clear specified partitions
Clear only the p1 and p2 partitions of the tbl table:
TRUNCATE TABLE tbl PARTITION(p1, p2);
Clear data without recovery
Clear data without recovery
Clear example_db.tbl while bypassing recycle-bin recovery:
TRUNCATE TABLE example_db.tbl FORCE;
Warning:
After using
FORCE, data cannot be recovered viaRECOVER. Confirm before executing.
Check cleanup constraints
Before executing TRUNCATE, confirm the following conditions:
- Only data is cleared, structure is retained: The table structure and partition definitions are retained; only data is cleared.
- Filter conditions are not supported: You can only clear the entire table or specified partitions.
WHEREconditions cannot be appended. - Table state must be NORMAL: A table that is performing
SCHEMA CHANGEcannot be truncated. - Affects ongoing imports: May cause running import tasks to fail. Execute during idle import periods.
- Check recovery before clearing data: Without
FORCE, old partitions enter the recycle bin. Recovery requires retained metadata, compatible schema, and no conflicting active partition. It is not an automatic undo of the table operation.
Recover retained partitions
Without FORCE, TRUNCATE retains the old partitions in the recycle bin and creates empty replacements. RECOVER TABLE does not undo this operation because the table still exists.
For a partitioned table, recovery uses RECOVER PARTITION. First inspect SHOW CATALOG RECYCLE BIN and confirm the old partition ID and retention with an administrator. A replacement partition with the same name or overlapping values must be moved or removed before recovery. Stop writes and preserve any new data before changing that partition.
The following statement illustrates recovery after the conflicting active partition has been handled. Replace <old-partition-id> with the retained ID:
RECOVER PARTITION p1 <old-partition-id> FROM example_db.tbl;
Schema or index changes after truncation can prevent recovery. For an unpartitioned table, or if you cannot safely resolve the partition conflict, contact VeloDB Cloud support before attempting recovery. See RECOVER for syntax and permissions.
Warning:
Retention is service-managed in VeloDB Cloud. Recovery is unavailable after
FORCEor after the retained metadata expires. Keep a separate backup or reloadable source for data you must restore.
Compare TRUNCATE with DELETE
Both can be used to delete data, but their applicable scenarios differ significantly:
| Comparison Item | TRUNCATE | DELETE |
|---|---|---|
| Deletion granularity | Entire table or entire partition | Row-level, with optional WHERE conditions |
| Filtering supported | No | Yes |
| Execution work | Replaces active partition metadata | Evaluates predicates or writes delete markers, depending on the table model |
| Execution speed | Depends on partition creation and metadata work | Related to the number of matched rows |
| Recoverable by default | Old partitions may be recovered subject to retention and conflicts (non-FORCE) | Not directly recoverable |
| Typical scenarios | Large-scale clearing, partition rebuild | Precise deletion of small amounts of data |
Choose a cleanup method:
- To clear an entire table or an entire partition, prefer
TRUNCATE. - To delete part of the data based on conditions, use
DELETE.
Resolve common questions
Retain the table structure
No. TRUNCATE only clears data. The table structure, partition definitions, indexes, and other metadata are all retained.
Check recovery eligibility
- Without
FORCE: Old partitions are eligible for recovery within retention, subject to name/range and schema checks. See Recover retained partitions. - With
FORCE: Recycle-bin recovery is unavailable. Physical cleanup runs asynchronously.
Wait for schema changes
No. The table state must be NORMAL; otherwise, the command reports an error. Wait for SCHEMA CHANGE to complete before executing.
Coordinate running loads
It may cause ongoing import tasks to fail. Execute during idle import periods, or pause related imports first.
Choose between TRUNCATE and DROP TABLE
TRUNCATE: Retains the table structure and only clears data.DROP TABLE: Deletes the table itself, including its structure and data.
Delete selected rows in a partition
No. TRUNCATE can only clear an entire table or specified partitions; it does not support row-level filtering. To delete by condition, use DELETE.
Explore related references
- RECOVER statement: Recover retained metadata when eligible.
- DELETE operation: Delete data based on conditions.