ΒΆ 9

Emptying a table

The table can be emptied with a TRUNCATE TABLE SQL statement or with a truncate() PHP client function.

Here is the syntax for the SQL statement:

TRUNCATE TABLE index_name [WITH RECONFIGURE]

When this statement is executed, it clears the RT table completely. It disposes the in-memory data, unlinks all the table data files, and releases the associated binary logs.

A table can also be emptied with DELETE FROM index WHERE id>0, but it's not recommended as it's slower than TRUNCATE.

SQL
TRUNCATE TABLE products;
Query OK, 0 rows affected (0.02 sec)
JSON
POST /cli -d "TRUNCATE TABLE products"
{
"total":0,
"error":"",
"warning":""
}
PHP
$params = [ 'index' => 'products' ];
$response = $client->indices()->truncate($params);
Array(
    [total] => 0
    [error] => 
    [warning] => 
)
Python
utilsApi.sql('TRUNCATE TABLE products')
{u'error': u'', u'total': 0, u'warning': u''}
javascript
res = await utilsApi.sql('TRUNCATE TABLE products');
{"total":0,"error":"","warning":""}
Java
utilsApi.sql("TRUNCATE TABLE products");
{total=0, error=, warning=}
C#
utilsApi.Sql("TRUNCATE TABLE products");
{total=0, error="", warning=""}

One of the possible uses of this command is before attaching a table.

When RECONFIGURE option is used new tokenization, morphology, and other text processing settings specified in the config take effect after the table gets cleared. In case the schema declaration in config is different from the table schema the new schema from config got applied after table get cleared.

With this option clearing and reconfiguring a table becomes one atomic operation.

SQL
TRUNCATE TABLE products with reconfigure;
Query OK, 0 rows affected (0.02 sec)
HTTP
POST /cli -d "TRUNCATE TABLE products with reconfigure"
{
"total":0,
"error":"",
"warning":""
}
PHP
$params = [ 'index' => 'products', 'with' => 'reconfigure' ];
$response = $client->indices()->truncate($params);
Array(
    [total] => 0
    [error] => 
    [warning] => 
)
Python
utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE')
{u'error': u'', u'total': 0, u'warning': u''}
javascript
res = await utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE');
{"total":0,"error":"","warning":""}
Java
utilsApi.sql("TRUNCATE TABLE products WITH RECONFIGURE");
{total=0, error=, warning=}
C#
utilsApi.Sql("TRUNCATE TABLE products WITH RECONFIGURE");
{total=0, error="", warning=""}