Skip to main content
Deletes data rows from a table based on the specified conditions. The table can be a partitioned or non-partitioned table. For Duplicate Key tables, Aggregate tables, and Unique Key tables, you can delete data from specified partitions. From v2.3, Primary Key tables support complete DELETE...WHERE semantics, which allows you to delete data rows based on the primary key, any column, or the results of a subquery. From v3.0, the database enriches the DELETE...WHERE semantics with multi-table joins and common table expressions (CTEs). If you need to join Primary Key tables with other tables in the database, you can reference these other tables in the USING clause or CTE.

Usage notes

  • You must have privileges on the table and database you want to perform DELETE.
  • Frequent DELETE operations are not recommended. If needed, perform such operations during off-peak hours.
  • The DELETE operation only deletes data in the table. The table remains. To drop the table, run DROP TABLE.
  • To prevent misoperations from deleting data in the entire table, you must specify the WHERE clause in the DELETE statement.
  • The deleted rows are not immediately cleaned. They are marked as “deleted” and will be temporarily saved in Segment. Physically, the rows are removed only after data version merge (compaction) is completed.
  • This operation also deletes data of the materialized views that reference this table.
  • You can use the system variable insert_timeout to set the timeout duration for DELETE operations.

Duplicate Key tables, Aggregate tables, and Unique Key tables

Syntax

Parameters

Limits and usage notes

  • For Duplicate Key tables, you can use any column as the DELETE condition. For Aggregate tables and Unique Key tables, only key columns can be used as the DELETE condition.
  • The conditions that you specify must be in the AND relation. If you want to specify conditions in OR relation, you must specify the conditions in separate DELETE statements.
  • For Duplicate Key tables, Aggregate tables, and Unique Key tables, the DELETE statement does not support using subquery results as conditions.

Impacts

After you execute the DELETE statement, the query performance of your cluster may deteriorate for a period of time (before compaction is completed). The degree of deterioration varies based on the number of conditions that you specify. A larger number of conditions indicates a higher degree of deterioration. Bulk removal: prefer TRUNCATE PARTITION over DELETE On Duplicate Key, Aggregate, and Unique Key tables, DELETE does not physically remove rows. It writes a delete predicate (a “delete marker”) attached to a new tablet version. Every subsequent read must scan the affected tablets, evaluate the delete predicates per row, and merge the result on the fly (merge-on-read). This cost persists until base compaction merges the delete predicates with the base data. If a partition has no new writes after DELETE, base compaction may not be triggered until base_compaction_interval_seconds_since_last_operation elapses (24 hours by default), so the merge-on-read penalty can stay active for a long time. For tables with hundreds of partitions and thousands of tablets, a simple SELECT ... LIMIT 1 can become orders of magnitude slower or even time out. For bulk removal of entire partitions (for example, during historical-data migration), prefer:
TRUNCATE PARTITION drops the partition’s data immediately by replacing its rowsets with an empty version, without producing delete markers and without waiting for compaction. If your workload issues DELETE frequently (rather than as a one-off bulk cleanup), consider modeling the table as a Primary Key table instead. Primary Key tables support row-level deletes with delete-vector / merge-on-write semantics and do not incur the per-read merge-on-read penalty of Duplicate / Aggregate / Unique Key tables. Starting from this version, the database returns a notice in the MySQL OK packet’s info field when DELETE runs against a Duplicate / Aggregate / Unique Key table to remind you of this trade-off. The notice can be turned off by setting the FE configuration enable_non_primary_key_delete_warning to false.

Examples

Create a table and insert data

The following example creates a partitioned Duplicate Key table.

Query data

Delete data

Delete data from a specified partition
Delete rows whose k1 values are 3 from the p1 partition.
Delete data from a specified partition using AND
Delete rows whose k1 values are greater than or equal to 3 and whose k2 values are "abc" from the p1 partition.
Delete data from all partitions
Delete rows whose k2 values are "abc" or "cba" from all partitions.

Primary Key tables

From v2.3, Primary Key tables support complete DELETE...WHERE semantics, which allows you to delete data rows based on the primary key, any column, or a subquery.

Syntax

Parameters

Limits and usage notes

  • Primary Key tables do not support deleting data from a specified partition, for example, DELETE FROM <table_name> PARTITION <partition_id> WHERE <where_condition>.
  • The following comparison operators are supported: =, >, <, >=, <=, !=, IN, NOT IN.
  • The following logical operators are supported: AND and OR.
  • You cannot use the DELETE statement to run concurrent DELETE operations or to delete data at data loading. If you perform such operations, the atomicity, consistency, isolation, and durability (ACID) of transactions may not be ensured.

Examples

Create a table and insert data

Create a Primary Key table named score_board:

Query data

Execute the following statement to insert data into the score_board table:

Delete data

Delete data by primary key
You can specify the primary key in the DELETE statement, so the database does not need to scan the entire table. Delete rows whose id values are 0 from the score_board table.
Delete data by condition
Example 1: Delete rows whose score values are 22 from the score_board table.
Example 2: Delete rows whose score values are less than 22 from the score_board table.
Example 3: Delete rows whose score values are less than 22 and whose name values are not Bob from the score_board table.
Delete data by subquery result
You can nest one or more subqueries in the DELETE statement and use the subquery results as conditions. Before you delete data, execute the following statement to create another table named users:
Insert data into the users table:
Nest a subquery to find the rows whose country values are China from the users table, and delete the rows, which have the same name values as the rows returned from the subquery, from the score_board table. You can use one of the following methods to fulfill the purpose:
  • Method 1
  • Method 2
Delete data by using multi-table join or CTE
To delete all movies produced by the producer “foo”, you can execute the following statement:
You can also use a CTE to rewrite the above statement to improve readability.

References

SHOW DELETE: queries historical DELETE operations that were successfully performed on Duplicate Key tables, Aggregate tables, and Unique Key tables.