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

> Documentation for Detach

# DETACH Statement

Makes the server "forget" about the existence of a table, a materialized view, a dictionary, or a database.

**Syntax**

```sql theme={null}
DETACH TABLE|VIEW|DICTIONARY|DATABASE [IF EXISTS] [db.]name [ON CLUSTER cluster] [PERMANENTLY] [SYNC]
```

Detaching does not delete the data or metadata of a table, a materialized view, a dictionary or a database. If an entity was not detached `PERMANENTLY`, on the next server launch the server will read the metadata and recall the table/view/dictionary/database again. If an entity was detached `PERMANENTLY`, there will be no automatic recall.

Whether a table, a dictionary or a database was detached permanently or not, in both cases you can reattach them using the [ATTACH](/reference/statements/attach) query.
System log tables can be also attached back (e.g. `query_log`, `text_log`, etc.). Other system tables can't be reattached. On the next server launch the server will recall those tables again.

`ATTACH MATERIALIZED VIEW` does not work with short syntax (without `SELECT`), but you can attach it using the `ATTACH TABLE` query.

Note that you can not detach permanently the table which is already detached (temporary). But you can attach it back and then detach permanently again.

Also, you can not [DROP](/reference/statements/drop#drop-table) the detached table, or [CREATE TABLE](/reference/statements/create/table) with the same name as detached permanently, or replace it with the other table with [RENAME TABLE](/reference/statements/rename) query.

The `SYNC` modifier executes the action without delay.

**Example**

Creating a table:

```sql title="Query" theme={null}
CREATE TABLE test ENGINE = MergeTree ORDER BY () AS SELECT * FROM numbers(10);
SELECT * FROM test;
```

```text title="Response" theme={null}
┌─number─┐
│      0 │
│      1 │
│      2 │
│      3 │
│      4 │
│      5 │
│      6 │
│      7 │
│      8 │
│      9 │
└────────┘
```

Detaching the table:

```sql title="Query" theme={null}
DETACH TABLE test;
SELECT * FROM test;
```

```text title="Response" theme={null}
Received exception from server (version 21.4.1):
Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table default.test does not exist.
```

<Note>
  In ClickHouse Cloud users should use the `PERMANENTLY` clause e.g. `DETACH TABLE <table> PERMANENTLY`. If this clause is not used, tables will be reattached on cluster restart e.g. during upgrades.
</Note>

**See Also**

* [Materialized View](/reference/statements/create/view#materialized-view)
* [Dictionaries](/reference/statements/create/dictionary)
