Will space be freed up after deleting a database in TiDB?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tidb删除库后空间会腾出来吗

| username: 特雷西-迈克-格雷迪

[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?

| username: 啦啦啦啦啦 | Original post link

Yes, you need to wait for GC.

| username: TiDBer_ZxWlj6A1 | Original post link

The GC has a default time interval of 10 minutes, which can also be modified.

| username: zhh_912 | Original post link

Sure, wait for the GC time.

| username: zhaokede | Original post link

Wait patiently for the space to be reclaimed.

| username: TiDBer_QYr0vohO | Original post link

Wait for GC to collect.

| username: 鱼跃龙门 | Original post link

Sure, you need to wait for the GC time to reclaim.

| username: Jellybean | Original post link

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.
| username: 濱崎悟空 | Original post link

Yes, but you need to wait for the GC to collect.

| username: Kongdom | Original post link

You can refer to the official documentation. Space will not be immediately freed after deletion; it is achieved through GC.

| username: TiDBer_HUfcQIJx | Original post link

GC will reclaim

| username: lemonade010 | Original post link

The default GC interval is 10 minutes, assuming the parameters haven’t been adjusted.

| username: 这里介绍不了我 | Original post link

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).

| username: zhanggame1 | Original post link

Truncate and drop will be released after the GC time has passed, but delete will take a long time and requires compaction to release.

| username: 扬仔_tidb | Original post link

After delete, TiKV will not release immediately.

| username: TIDB-Learner | Original post link

Deletion only marks the data, and the space will be cleaned up by the GC process.

| username: TiDBer_H5NdJb5Q | Original post link

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?

| username: 迪迦奥特曼 | Original post link

You need to wait for GC or manually perform space reclamation.
GC related:

| username: TiDBer_H5NdJb5Q | Original post link

Thank you, expert. This solved my question.

| username: 随缘天空 | Original post link

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.