> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentação para manipulações de TTL da tabela

# Manipulações de TTL da tabela

<Note>
  Se estiver procurando detalhes sobre como usar TTL para gerenciar dados antigos, consulte o guia do usuário [Gerenciar dados com TTL](/pt-BR/concepts/features/operations/delete/ttl). A documentação abaixo mostra como alterar ou remover uma regra de TTL existente.
</Note>

<div id="modify-ttl">
  ## ALTERAR TTL
</div>

Você pode alterar o [TTL da tabela](/pt-BR/reference/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl) com uma consulta no seguinte formato:

```sql theme={null}
ALTER TABLE [db.]table_name [ON CLUSTER cluster] MODIFY TTL ttl_expression;
```

<div id="remove-ttl">
  ## REMOVER TTL
</div>

A propriedade TTL pode ser removida da tabela com a consulta a seguir:

```sql theme={null}
ALTER TABLE [db.]table_name [ON CLUSTER cluster] REMOVE TTL
```

**Exemplo**

Considere a tabela com `TTL` de tabela:

```sql theme={null}
CREATE TABLE table_with_ttl
(
    event_time DateTime,
    UserID UInt64,
    Comment String
)
ENGINE MergeTree()
ORDER BY tuple()
TTL event_time + INTERVAL 3 MONTH
SETTINGS min_bytes_for_wide_part = 0;

INSERT INTO table_with_ttl VALUES (now(), 1, 'username1');

INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
```

Execute `OPTIMIZE` para forçar a limpeza do `TTL`:

```sql theme={null}
OPTIMIZE TABLE table_with_ttl FINAL;
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
```

A segunda linha foi excluída da tabela.

```text theme={null}
┌─────────event_time────┬──UserID─┬─────Comment──┐
│   2020-12-11 12:44:57 │       1 │    username1 │
└───────────────────────┴─────────┴──────────────┘
```

Agora, remova o `TTL` da tabela com a seguinte consulta:

```sql theme={null}
ALTER TABLE table_with_ttl REMOVE TTL;
```

Reinsira a linha excluída e force novamente a limpeza do `TTL` com `OPTIMIZE`:

```sql theme={null}
INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
OPTIMIZE TABLE table_with_ttl FINAL;
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
```

O `TTL` não está mais lá, então a segunda linha não é excluída:

```text theme={null}
┌─────────event_time────┬──UserID─┬─────Comment──┐
│   2020-12-11 12:44:57 │       1 │    username1 │
│   2020-08-11 12:44:57 │       2 │    username2 │
└───────────────────────┴─────────┴──────────────┘
```

**Veja também**

* Mais informações sobre a [expressão TTL](/pt-BR/reference/statements/create/table#ttl-expression).
* Modifique a coluna [com TTL](/pt-BR/reference/statements/alter/ttl).
