Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: tidb删除库后空间会腾出来吗
[TiDB Usage Environment] Production Environment
[TiDB Version] 5.2.0
[Encountered Problem: Problem Phenomenon and Impact]
Will space be freed up after deleting a database in TiDB? Is space cleanup achieved through GC?
Yes, you need to wait for GC.
The GC has a default time interval of 10 minutes, which can also be modified.
Sure, wait for the GC time.
Wait patiently for the space to be reclaimed.
Sure, you need to wait for the GC time to reclaim.
First, using DELETE
, TRUNCATE
, and DROP
statements to delete data will not immediately release space.
When will the space be released? Specifically:
- For
TRUNCATE
and DROP
operations, after reaching the GC time (default 10 minutes), the GC mechanism will delete the data and release the space.
- For
DELETE
operations, the GC mechanism will mark the data for deletion, and the space will be released during subsequent compaction.
Yes, but you need to wait for the GC to collect.
You can refer to the official documentation. Space will not be immediately freed after deletion; it is achieved through GC.
The default GC interval is 10 minutes, assuming the parameters haven’t been adjusted.
TiDB uses a Multi-Version Concurrency Control (MVCC) mechanism. When new data overwrites old data, the old data is not replaced but retained alongside the new data, distinguished by timestamps. To allow concurrent transactions to view earlier versions of the data, TiDB does not immediately reclaim space when data is deleted but waits for a period before performing garbage collection (GC). To configure the retention period for historical data, you can modify the system variable tidb_gc_life_time
(default value is 10m0s).
Truncate and drop will be released after the GC time has passed, but delete will take a long time and requires compaction to release.
After delete, TiKV will not release immediately.
Deletion only marks the data, and the space will be cleaned up by the GC process.
Borrowing the original poster’s thread to ask a question: If a transaction runs for more than 10 minutes, and during this period another transaction has already modified and committed a piece of data that is being read, will the GC delete the old data at this time, or will it wait until the first transaction is committed before deleting it?
You need to wait for GC or manually perform space reclamation.
GC related:
Thank you, expert. This solved my question.
None of these three commands will immediately release space. However, unlike the delete command, the other two commands will release space after the GC time, which is set to ten minutes by default. Due to TiDB’s MVCC mechanism, the delete command will actually increase disk space usage in the short term. However, if new data is added, the space freed by the delete command can be reused. The space will only be truly released during subsequent region operations.