---
metadata:
  - name: generator
    content: Diplodoc Platform v5.56.0
alternate:
  - https://ydb.tech/docs/en/yql/reference/syntax/alter_table/compact.md?version=main
  - https://ydb.tech/docs/ru/yql/reference/syntax/alter_table/compact.md?version=main
  - href: en/yql/reference/syntax/alter_table/compact.md
    type: text/markdown
    title: Markdown version
  - href: ../../../../llms.txt
    type: text/markdown
    title: llms.txt
sourcePath: en/core/yql/reference/syntax/alter_table/compact.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt

# Running forced table compaction


```yql
ALTER TABLE table_name COMPACT [WITH (key = value [, ...])];
```



<!-- source: en/_includes/only_allow_for_oltp_note.md -->
{% note warning %}

<!-- source: en/_includes/only_allow_for_oltp_text.md -->
Supported only for [row-oriented](https://ydb.tech/docs/en/concepts/datamodel/table.md?version=main#row-oriented-tables) tables.
<!-- endsource: en/_includes/only_allow_for_oltp_text.md -->

{% endnote %}
<!-- endsource: en/_includes/only_allow_for_oltp_note.md -->


YDB automatically performs [compaction](https://ydb.tech/docs/en/concepts/glossary.md?version=main#compaction) of tables and usually does not require manual intervention. However, using the `ALTER TABLE ... COMPACT` command, you can explicitly start compaction of data in a given table without waiting for automatic compaction. The command is useful, for example, in the following scenarios:

* Bulk data deletion was performed and you want to free up the occupied space faster.
* Compression settings or [column groups](https://ydb.tech/docs/en/yql/reference/syntax/alter_table/family.md?version=main) were changed, and you want to apply them to existing data faster.

The command creates a background operation and synchronously waits for its completion. The wait can be interrupted, and the operation will continue running in the background. Background compaction operations can be managed via the [CLI](https://ydb.tech/docs/en/reference/ydb-cli/index.md?version=main) and [YDB UI](https://ydb.tech/docs/en/reference/ydb-ui/index.md?version=main).

## Parameters {#options}

The following parameters can be specified in the `WITH` block:

* `CASCADE` — whether to compact the table together with its [secondary indexes](https://ydb.tech/docs/en/yql/reference/syntax/alter_table/indexes.md?version=main). Valid values: `TRUE` and `FALSE`. Default value: `FALSE` (only the table itself is compacted).
* `PARALLEL` — the maximum number of partitions that can be compacted simultaneously within this operation. Valid values are positive integers. Default value: 1. The overall limit on concurrently running manual compactions for the entire database is set separately in the YDB cluster configuration.

## Behavior {#behavior}

* You can monitor the progress of running compaction operations using the [CLI command](https://ydb.tech/docs/en/reference/ydb-cli/operation-list.md?version=main) `ydb operation list compaction`. Using the corresponding commands, you can [get the status of a specific operation](https://ydb.tech/docs/en/reference/ydb-cli/operation-get.md?version=main), [cancel an operation](https://ydb.tech/docs/en/reference/ydb-cli/operation-cancel.md?version=main), or [delete the record of a completed operation](https://ydb.tech/docs/en/reference/ydb-cli/operation-forget.md?version=main).
* You can also view and manage operations using the [YDB UI](https://ydb.tech/docs/en/reference/ydb-ui/index.md?version=main). To do this, go to the database page, open the `Operations` tab, and select the operation type `Compaction`.
* If a compaction operation is already running for the same table (or for one of its secondary indexes when `CASCADE = TRUE`), the launch will fail with an error. You need to either wait for the previous operation to complete or cancel it.
* Running the command requires the same permissions as other `ALTER TABLE` actions. Concurrent table schema changes are not blocked.
* Forced compaction also materializes borrowed data. As a result, the total data volume in the database may increase. The borrowing mechanism is used, for example, to implement [CoW](https://en.wikipedia.org/wiki/Copy-on-write) [table copying](https://ydb.tech/docs/en/reference/ydb-cli/tools-copy.md?version=main), so when compacting copied tables, their volume may eventually increase by the size of the original table.

## Examples {#examples}

Run compaction of only the main table:


```yql
ALTER TABLE series COMPACT;
```


Run compaction of the table together with all its secondary indexes:


```yql
ALTER TABLE series COMPACT WITH (CASCADE = TRUE);
```


Run compaction of only the impl table of a specific secondary index:


```yql
ALTER TABLE `series/idx_release/indexImplTable` COMPACT;
```


Run compaction of the table, setting the number of simultaneously processed partitions:


```yql
ALTER TABLE series COMPACT WITH (PARALLEL = 5);
```
