All Products
Search
Document Center

MaxCompute:DELETE

Last Updated:Dec 25, 2025

The DELETE statement deletes one or more rows that meet specified conditions from a Transactional table or a Delta Table.

Prerequisites

Before you run a DELETE or UPDATE statement, you must have Select and Update permissions on the target Transactional table or Delta Table. For more information about authorization, see MaxCompute permissions.

Limits

  • The DELETE statement, UPDATE statement, and a transactional table or Delta table on which the DELETE or UPDATE statement is executed have the following limits:

    Note

    For more information, see Parameters for Transaction Table and Delta Table.

    • Before you execute the UPDATE, DELETE, or INSERT OVERWRITE statement on important data in transactional tables, you must execute the SELECT and INSERT statements to back up the data to other tables.

    • You cannot execute the UPDATE statement to change values in the primary key column of a Delta table.

Precautions

When you use DELETE or UPDATE statement to delete or update data in tables or partitions of the tables, take note of the following items:

  • In specific scenarios, you may want to execute the DELETE or UPDATE statement for a small amount of data in a table and infrequently perform read and other operations in subsequent procedures. To reduce the storage space that is occupied by the table, we recommend that you merge the base files with all delta files after you execute the DELETE or UPDATE statement for the table several times. For more information, see ALTER TABLE COMPACT.

  • In specific scenarios, you may want to delete or update more than 5% of the data in a table or a partition of the table at a low frequency and perform frequent read operations in subsequent procedures. We recommend that you execute the INSERT OVERWRITE or INSERT INTO statement in such scenarios. For more information, see Insert data into or overwrite data in a table or a static partition (INSERT INTO and INSERT OVERWRITE).

    For example, you want to perform delete or update operations for 10% of data 10 times each day. In this case, we recommend that you estimate the total cost and the consumption of the subsequent read performance if you execute the DELETE or UPDATE statement on the table. Then, compare the estimated result with that of executing the INSERT OVERWRITE or INSERT INTO statement. This helps you choose an efficient method.

  • Each time you execute the DELETE statement on a table, a delta file is automatically generated. As a result, the occupied storage space may not be reduced. If you want to execute the DELETE statement to delete data to reduce storage usage, you can merge the base files with all delta files. For more information, see ALTER TABLE COMPACT.

  • MaxCompute executes multiple DELETE and UPDATE statements in jobs at a time. Each statement consumes resources and incurs fees. We recommend that you delete or update a batch of data at a time. For example, if you run a Python script to generate and submit a large number of row-level update jobs, and each statement is executed for only one row or a small number of rows of data, each statement incurs fees that correspond to the amount of input data scanned by the SQL statement and consumes the related computing resources. When multiple statements are accumulated, the costs are significantly increased and the system efficiency is reduced. Sample statements:

    • We recommend that you execute the following statement:

      UPDATE table1 SET col1= (SELECT value1 FROM table2 WHERE table1.id = table2.id AND table1.region = table2.region);
    • We recommend that you do not execute the following statements:

      UPDATE table1 SET col1=1 WHERE id='2021063001'AND region='beijing';                  
      UPDATE table1 SET col1=2 WHERE id='2021063002'AND region='beijing';

Syntax

DELETE FROM <table_name> [[AS] alias] [WHERE <condition>];

Parameters

Parameter

Required

Description

table_name

Yes

The name of the Transactional or Delta table for the DELETE operation.

alias

No

The alias for the table.

where_condition

No

The WHERE clause to filter data based on a condition. For more information, see WHERE Clause (WHERE_condition). If you omit this clause, all data in the table is deleted.

Examples

  • Example 1: Create a non-partitioned table named acid_delete, insert data, and then run the DELETE statement to delete rows that meet a specified condition.

    --Create a Transactional table named acid_delete.
    CREATE TABLE IF NOT EXISTS acid_delete (id BIGINT) TBLPROPERTIES ("transactional"="true");
    
    --Insert data.
    INSERT OVERWRITE TABLE acid_delete VALUES (1), (2), (3), (2);
    
    --View the inserted data.
    SELECT * FROM acid_delete;
    
    +------------+
    | id         |
    +------------+
    | 1          |
    | 2          |
    | 3          |
    | 2          |
    +------------+
    
    -- Delete the rows where id is 2. If you run this statement in the MaxCompute client (odpscmd), you must enter yes or no to confirm the deletion.
    DELETE FROM acid_delete WHERE id = 2;
    --This is equivalent to the preceding statement.
    DELETE FROM acid_delete ad WHERE ad.id = 2;
    
    --View the result. The table contains only the rows where id is 1 or 3.
    SELECT * FROM acid_delete;
    
    +------------+
    | id         |
    +------------+
    | 1          |
    | 3          |
    +------------+
  • Example 2: Create a partitioned table named acid_delete_pt, insert data, and then run the DELETE statement to delete rows that meet a specified condition.

    --Create a Transactional table named acid_delete_pt.  
    CREATE TABLE IF NOT EXISTS acid_delete_pt (id BIGINT) PARTITIONED BY (ds STRING) TBLPROPERTIES ("transactional"="true");
    
    --Add partitions.
    ALTER TABLE acid_delete_pt ADD IF NOT EXISTS PARTITION (ds = '2019');
    ALTER TABLE acid_delete_pt ADD IF NOT EXISTS PARTITION (ds = '2018');
    
    --Insert data.
    INSERT OVERWRITE TABLE acid_delete_pt PARTITION (ds = '2019') VALUES (1), (2), (3);
    INSERT OVERWRITE TABLE acid_delete_pt PARTITION (ds = '2018') VALUES (1), (2), (3);
    
    --View the inserted data.
    SELECT * FROM acid_delete_pt;
    
    +------------+------------+
    | id         | ds         |
    +------------+------------+
    | 1          | 2018       |
    | 2          | 2018       |
    | 3          | 2018       |
    | 1          | 2019       |
    | 2          | 2019       |
    | 3          | 2019       |
    +------------+------------+
    
    --Delete the rows where the partition is 2019 and id is 2. If you run this statement in the MaxCompute client (odpscmd), you must enter yes or no to confirm the deletion.
    DELETE FROM acid_delete_pt WHERE ds = '2019' AND id = 2;
    
    --View the result. The rows where the partition is 2019 and id is 2 are deleted.
    SELECT * FROM acid_delete_pt;
    
    +------------+------------+
    | id         | ds         |
    +------------+------------+
    | 1          | 2018       |
    | 2          | 2018       |
    | 3          | 2018       |
    | 1          | 2019       |
    | 3          | 2019       |
    +------------+------------+
  • Example 3: Create a destination table named acid_delete_t and an associated table named acid_delete_s. Then, delete the rows that meet the specified conditions from the destination table based on the associated table. Sample statements:

    --Create a target Transactional table named acid_delete_t and an associated table named acid_delete_s. 
    CREATE TABLE IF NOT EXISTS acid_delete_t (id INT, value1 INT, value2 INT) TBLPROPERTIES ("transactional"="true");
    CREATE TABLE IF NOT EXISTS acid_delete_s (id INT, value1 INT, value2 INT);
    
    --Insert data.
    INSERT OVERWRITE TABLE acid_delete_t VALUES (2, 20, 21), (3, 30, 31), (4, 40, 41);
    INSERT OVERWRITE TABLE acid_delete_s VALUES (1, 100, 101), (2, 200, 201), (3, 300, 301);
    
    --Delete rows from the acid_delete_t table whose id does not match any id in the acid_delete_s table. If you run this statement in the MaxCompute client (odpscmd), you must enter yes or no to confirm the deletion.
    DELETE FROM acid_delete_t WHERE NOT EXISTS (SELECT * FROM acid_delete_s WHERE acid_delete_t.id = acid_delete_s.id);
    --This is equivalent to the preceding statement.
    DELETE FROM acid_delete_t a WHERE NOT EXISTS (SELECT * FROM acid_delete_s b WHERE a.id = b.id);
    
    --View the result. The table contains only the rows where id is 2 or 3.
    SELECT * FROM acid_delete_t;
    
    +------------+------------+------------+
    | id         | value1     | value2     |
    +------------+------------+------------+
    | 2          | 20         | 21         |
    | 3          | 30         | 31         |
    +------------+------------+------------+
  • Example 4: Create a Delta Table named mf_dt, insert data, and then run the DELETE statement to delete rows that meet a specified condition.

    --Create a target Delta Table named mf_dt. 
    CREATE TABLE IF  NOT EXISTS mf_dt (pk BIGINT  NOT NULL PRIMARY KEY, 
                      val  BIGINT NOT NULL) 
                      PARTITIONED BY(dd STRING, hh STRING) 
                      tblproperties ("transactional"="true");
    
    --Insert data.
    INSERT OVERWRITE TABLE mf_dt PARTITION (dd='01', hh='02') VALUES (1, 1), (2, 2), (3, 3);
    
    --View the inserted data.                 
    SELECT * FROM mf_dt WHERE dd='01' AND hh='02';
    
    --The following result is returned.
    +------------+------------+----+----+
    | pk         | val        | dd | hh |
    +------------+------------+----+----+
    | 1          | 1          | 01 | 02 |
    | 3          | 3          | 01 | 02 |
    | 2          | 2          | 01 | 02 |
    +------------+------------+----+----+
    
    --Delete the rows where val is 2 and the partition is dd='01' and hh='02'.
    DELETE FROM mf_dt WHERE val = 2  AND dd='01' AND hh='02';
    
    --View the result. The table contains only the rows where val is 1 or 3.
    SELECT * FROM mf_dt WHERE dd='01' AND hh='02';
    
    --The following result is returned.
    +------------+------------+----+----+
    | pk         | val        | dd | hh |
    +------------+------------+----+----+
    | 1          | 1          | 01 | 02 |
    | 3          | 3          | 01 | 02 |
    +------------+------------+----+----+

Related statements

  • UPDATE: updates the values of one or more columns of the rows that meet the specified conditions in partitioned transactional tables or non-partitioned transactional tables.

  • ALTER TABLE: merges transactional table files.