TRUNCATE Statements

Batch

TRUNCATE statements are used to delete all rows from a table without dropping the table itself.

Attention Currently, TRUNCATE statement is supported in batch mode, and it requires the target table connector implements the SupportsTruncate interface to support the row-level delete. An exception will be thrown if trying to TRUNCATE a table which has not implemented the related interface.

Run a TRUNCATE statement

Java

TRUNCATE statement can be executed with the executeSql() method of the TableEnvironment. The executeSql() method will throw an exception when there is any error for the operation.

The following examples show how to run a TRUNCATE statement in TableEnvironment.

Scala

TRUNCATE statements can be executed with the executeSql() method of the TableEnvironment. The executeSql() will throw an exception when there is any error for the operation.

The following examples show how to run a single TRUNCATE statement in TableEnvironment.

Python

TRUNCATE statements can be executed with the execute_sql() method of the TableEnvironment. The execute_sql() will throw an exception when there is any error for the operation.

The following examples show how to run a single TRUNCATE statement in TableEnvironment.

SQL CLI

TRUNCATE statement can be executed in SQL CLI.

The following examples show how to run a TRUNCATE statement in SQL CLI.

Java

  1. EnvironmentSettings settings = EnvironmentSettings.newInstance().inBatchMode().build();
  2. TableEnvironment tEnv = TableEnvironment.create(settings);
  3. // register a table named "Orders"
  4. tEnv.executeSql("CREATE TABLE Orders (`user` STRING, product STRING, amount INT) WITH (...)");
  5. // insert values
  6. tEnv.executeSql("INSERT INTO Orders VALUES ('Lili', 'Apple', 1), ('Jessica', 'Banana', 2), ('Mr.White', 'Chicken', 3)").await();
  7. tEnv.executeSql("SELECT * FROM Orders").print();
  8. // +--------------------------------+--------------------------------+-------------+
  9. // | user | product | amount |
  10. // +--------------------------------+--------------------------------+-------------+
  11. // | Lili | Apple | 1 |
  12. // | Jessica | Banana | 2 |
  13. // | Mr.White | Chicken | 3 |
  14. // +--------------------------------+--------------------------------+-------------+
  15. // 3 rows in set
  16. // truncate the table "Orders"
  17. tEnv.executeSql("TRUNCATE TABLE Orders").await();
  18. tEnv.executeSql("SELECT * FROM Orders").print();
  19. // Empty set

Scala

  1. val env = StreamExecutionEnvironment.getExecutionEnvironment()
  2. val settings = EnvironmentSettings.newInstance().inBatchMode().build()
  3. val tEnv = StreamTableEnvironment.create(env, settings)
  4. // register a table named "Orders"
  5. tEnv.executeSql("CREATE TABLE Orders (`user` STRING, product STRING, amount INT) WITH (...)")
  6. // insert values
  7. tEnv.executeSql("INSERT INTO Orders VALUES ('Lili', 'Apple', 1), ('Jessica', 'Banana', 2), ('Mr.White', 'Chicken', 3)").await()
  8. tEnv.executeSql("SELECT * FROM Orders").print()
  9. // +--------------------------------+--------------------------------+-------------+
  10. // | user | product | amount |
  11. // +--------------------------------+--------------------------------+-------------+
  12. // | Lili | Apple | 1 |
  13. // | Jessica | Banana | 2 |
  14. // | Mr.White | Chicken | 3 |
  15. // +--------------------------------+--------------------------------+-------------+
  16. // 3 rows in set
  17. // truncate the table "Orders"
  18. tEnv.executeSql("TRUNCATE TABLE Orders").await()
  19. tEnv.executeSql("SELECT * FROM Orders").print()
  20. // Empty set

Python

  1. env_settings = EnvironmentSettings.in_batch_mode()
  2. table_env = TableEnvironment.create(env_settings)
  3. # register a table named "Orders"
  4. table_env.execute_sql("CREATE TABLE Orders (`user` STRING, product STRING, amount INT) WITH (...)")
  5. # insert values
  6. table_env.execute_sql("INSERT INTO Orders VALUES ('Lili', 'Apple', 1), ('Jessica', 'Banana', 2), ('Mr.White', 'Chicken', 3)").wait()
  7. table_env.execute_sql("SELECT * FROM Orders").print()
  8. # +--------------------------------+--------------------------------+-------------+
  9. # | user | product | amount |
  10. # +--------------------------------+--------------------------------+-------------+
  11. # | Lili | Apple | 1 |
  12. # | Jessica | Banana | 2 |
  13. # | Mr.White | Chicken | 3 |
  14. # +--------------------------------+--------------------------------+-------------+
  15. # 3 rows in set
  16. # truncate the table "Orders"
  17. table_env.execute_sql("TRUNCATE TABLE Orders").wait()
  18. table_env.execute_sql("SELECT * FROM Orders").print()
  19. # Empty set

SQL CLI

  1. Flink SQL> SET 'execution.runtime-mode' = 'batch';
  2. [INFO] Session property has been set.
  3. Flink SQL> CREATE TABLE Orders (`user` STRING, product STRING, amount INT) with (...);
  4. [INFO] Execute statement succeed.
  5. Flink SQL> INSERT INTO Orders VALUES ('Lili', 'Apple', 1), ('Jessica', 'Banana', 1), ('Mr.White', 'Chicken', 3);
  6. [INFO] Submitting SQL update statement to the cluster...
  7. [INFO] SQL update statement has been successfully submitted to the cluster:
  8. Job ID: bd2c46a7b2769d5c559abd73ecde82e9
  9. Flink SQL> SELECT * FROM Orders;
  10. user product amount
  11. Lili Apple 1
  12. Jessica Banana 2
  13. Mr.White Chicken 3
  14. Flink SQL> TRUNCATE TABLE Orders;
  15. [INFO] Execute statement succeed.
  16. Flink SQL> SELECT * FROM Orders;
  17. // Empty set

Syntax

  1. TRUNCATE TABLE [catalog_name.][db_name.]table_name