Skip to main content

Delete Data

Deleting data is a feature provided by TDengine that allows users to remove data records from specified tables or supertables based on a specified time range, making it convenient to clean up abnormal data generated due to equipment failures and other reasons.

Note: Deleting data does not immediately free up the disk space occupied by the table; instead, the data in the table is marked as deleted and will not appear in queries. However, the release of disk space will be delayed until the system automatically or the user manually reorganizes the data.

Syntax:

DELETE FROM [ db_name. ] tb_name [WHERE condition];

Function: Delete data records from specified tables or supertables.

Parameters:

  • db_name: An optional parameter specifying the name of the database where the table to be deleted resides. If not specified, it will operate in the current database.
  • tb_name: A mandatory parameter specifying the name of the table from which data will be deleted. This can be a basic table, subtable, or supertable.
  • condition: An optional parameter specifying the filter condition for deleting data. If no filter condition is specified, all data in the table will be deleted; use with caution. Notably, the where condition here only supports filtering on the first column (the timestamp column).

Special Note:

Once data is deleted, it cannot be recovered; please use this command with caution. To ensure that the data to be deleted is indeed the correct data, it is recommended to first use the SELECT statement along with the WHERE clause to view the data that will be deleted. Confirm that the selection is accurate before executing the DELETE command.

Example:

meters is a supertable, and groupid is an int type tag column. Now, to delete all data in the meters table with a timestamp less than 2021-10-01 10:40:00.100, the SQL is as follows:

DELETE FROM meters WHERE ts < '2021-10-01 10:40:00.100';

After execution, the result will display:

Deleted 102000 row(s) from 1020 table(s) (0.421950s)

This indicates that a total of 102,000 rows of data were deleted from 1,020 subtables.