DELETE

Remove rows from a table. You can use a WHERE clause to specify which rows should be removed. If you need to use a subquery(s) or additional table(s) to identify the rows to be removed, specify the subquery(s) or table(s) in a USING clause.

Important

Unlike TRUNCATE TABLE, this command does not delete the external file load history. If you delete rows loaded into the table from a staged file, you cannot load the data from that file again unless you modify the file and stage it again.

Syntax

DELETE FROM <table_name>
            [ USING <additional_table_or_query> [, <additional_table_or_query> ] ]
            [ WHERE <condition> ]

Required parameters

table_name

Specifies the table from which rows are removed.

Optional parameters

USING additional_table_or_query [, ... ]

If you need to refer to additional tables in the WHERE clause to help identify the rows to be removed, then specify those table names in the USING clause. You can also use the USING clause to specify subqueries that identify the rows to be removed.

If you specify a subquery, then put the subquery in parentheses.

If you specify more than one table or query, use a comma to separate them.

WHERE condition

Specifies a condition to use to select rows for removal. If this parameter is omitted, all rows in the table are removed, but the table remains.

Usage notes

  • When deleting based on a JOIN (by specifying a USING clause), it is possible that a row in the target table joins against several rows in the USING table(s). If the DELETE condition is satisfied for any of the joined combinations, the target row is deleted.

    For example, given tables tab1 and tab2 with columns (k number, v number):

    select * from tab1;
    
    -------+-------+
    k   |   v   |
    -------+-------+
    0   |   10  |
    -------+-------+
    
    Select * from tab2;
    
    -------+-------+
    k   |   v   |
    -------+-------+
    0   |   20  |
    0   |   30  |
    -------+-------+
    

    If you run the following query, the row in tab1 is joined against both rows of tab2:

    DELETE FROM tab1 USING tab2 WHERE tab1.k = tab2.k
    

    Because at least one joined pair satisfies the condition, the row is deleted. As a result, after the statement completes, tab1 is empty.

Protecting against accidental deletes

A DELETE statement is permanent unless you can recover the removed rows. Snowflake provides several features that help you avoid or reverse an accidental delete:

  • Time Travel: For the data retention period, you can query a table as it was before the delete by using AT | BEFORE (AT or BEFORE), and then restore the rows. For more information, see Understanding & using Time Travel.
  • Zero-copy cloning: You can clone a table, including as it existed at a past Time Travel point, and keep the clone as a safety copy before you delete. For more information, see Cloning considerations.
  • Backups: A backup preserves a point-in-time copy of a table that you can restore from later. For more information, see Backups for disaster recovery and immutable storage.
  • Fail-safe: After the Time Travel retention period ends, Snowflake keeps historical data for a further seven days for disaster recovery. Only Snowflake can recover data in Fail-safe, so you can’t depend on it for routine recovery. For more information, see Understanding and viewing Fail-safe.
  • Transactions: If you run DELETE inside an explicit transaction, you can undo it with ROLLBACK before you commit. For more information, see BEGIN.
  • UNDROP: If you dropped a table rather than deleting rows from it, use UNDROP TABLE.
  • Archiving rows: You can copy the rows to an archive table with INSERT … SELECT before you delete them.
  • Streams: A stream records the changes made to a table, including deletes. For more information, see Introduction to streams.
  • Access control: Grant the DELETE privilege on a table only to roles that need it.

For examples, see Recovering from an accidental delete.

Examples

Suppose that an organization that leases bicycles uses the following tables:

  • The table named leased_bicycles lists the bicycles that were leased out.
  • The table named returned_bicycles lists bicycles that have been returned recently. These bicycles need be removed from the table of leased bicycles.

Create tables:

CREATE TABLE leased_bicycles (bicycle_id INTEGER, "customer_id" INTEGER);
CREATE TABLE returned_bicycles (bicycle_id INTEGER);

Load data:

INSERT INTO leased_bicycles (bicycle_ID, "customer_id") VALUES
    (101, 1111),
    (102, 2222),
    (103, 3333),
    (104, 4444),
    (105, 5555);
INSERT INTO returned_bicycles (bicycle_ID) VALUES
    (102),
    (104),
    (110);

This example shows how to use the WHERE clause to delete a specified row(s). This example deletes by customer_id:

DELETE FROM leased_bicycles WHERE "customer_id" = 5555;
+------------------------+
| number of rows deleted |
|------------------------|
|                      1 |
+------------------------+

Show the data after the delete:

SELECT * FROM leased_bicycles ORDER BY bicycle_ID;
+------------+-------------+
| BICYCLE_ID | customer_id |
|------------+-------------|
|        101 |        1111 |
|        102 |        2222 |
|        103 |        3333 |
|        104 |        4444 |
+------------+-------------+

This example shows how to use the USING clause to specify rows to be deleted. This USING clause specifies the returned_bicycles table, which lists the IDs of the bicycles to be deleted from the leased_bicycles table. The WHERE clause joins the leased_bicycles table to the returned_bicycles table, and the rows in leased_bicycles that have the same bicycle_ID as the corresponding rows in returned_bicycles are deleted.

BEGIN WORK;
DELETE FROM leased_bicycles 
    USING returned_bicycles
    WHERE leased_bicycles.bicycle_ID = returned_bicycles.bicycle_ID;
TRUNCATE TABLE returned_bicycles;
COMMIT WORK;

(To avoid trying to remove the same rows again in the future when it might be unnecessary or inappropriate, the returned_bicycles table is truncated as part of the same transaction.)

Show the data after the delete:

SELECT * FROM leased_bicycles ORDER BY bicycle_ID;
+------------+-------------+
| BICYCLE_ID | customer_id |
|------------+-------------|
|        101 |        1111 |
|        103 |        3333 |
+------------+-------------+

Now suppose that another bicycle(s) is returned:

INSERT INTO returned_bicycles (bicycle_ID) VALUES (103);

The following query shows a USING clause that contains a subquery (rather than a table) to specify which bicycle_IDs to remove from the leased_bicycles table:

BEGIN WORK;
DELETE FROM leased_bicycles 
    USING (SELECT bicycle_ID AS bicycle_ID FROM returned_bicycles) AS returned
    WHERE leased_bicycles.bicycle_ID = returned.bicycle_ID;
TRUNCATE TABLE returned_bicycles;
COMMIT WORK;

Show the data after the delete:

SELECT * FROM leased_bicycles ORDER BY bicycle_ID;
+------------+-------------+
| BICYCLE_ID | customer_id |
|------------+-------------|
|        101 |        1111 |
+------------+-------------+

Recovering from an accidental delete

The following examples use the leased_bicycles table as originally loaded, with all five rows (bicycles 101 through 105) present. Run the load statements again to reset the table if needed.

Recover deleted rows with Time Travel

Record the time before you delete, and then delete the rows:

SET before_delete = CURRENT_TIMESTAMP();

DELETE FROM leased_bicycles WHERE "customer_id" = 5555;

To recover the deleted rows, insert them back from the table as it was before the delete. Alternatively, use BEFORE(STATEMENT => '<query_id>') with the query ID of the DELETE statement:

INSERT INTO leased_bicycles
  SELECT * FROM leased_bicycles AT(TIMESTAMP => $before_delete)
  WHERE "customer_id" = 5555;
SELECT * FROM leased_bicycles WHERE "customer_id" = 5555;
+------------+-------------+
| BICYCLE_ID | customer_id |
|------------+-------------|
|        105 |        5555 |
+------------+-------------+

Recover by cloning the table from before the delete

Create a clone of the table as it was before the delete, replacing <query_id> with the query ID of the DELETE statement:

CREATE TABLE leased_bicycles_restored CLONE leased_bicycles
  BEFORE(STATEMENT => '<query_id>');

You can then copy rows from the clone back into the original table, or swap the two tables by using ALTER TABLE … SWAP WITH.

Archive rows before you delete them

Copy the rows to an archive table, and then delete them. Running both statements in one transaction ensures that the delete happens only if the archive succeeds:

CREATE TABLE leased_bicycles_archive LIKE leased_bicycles;

BEGIN;
INSERT INTO leased_bicycles_archive
  SELECT * FROM leased_bicycles WHERE "customer_id" = 5555;
DELETE FROM leased_bicycles WHERE "customer_id" = 5555;
COMMIT;

Undo a delete with ROLLBACK

Run the delete in an explicit transaction, check the result, and then roll back to undo it:

BEGIN;
DELETE FROM leased_bicycles WHERE "customer_id" = 5555;
SELECT COUNT(*) FROM leased_bicycles WHERE "customer_id" = 5555;
+----------+
| COUNT(*) |
|----------|
|        0 |
+----------+
ROLLBACK;
SELECT COUNT(*) FROM leased_bicycles WHERE "customer_id" = 5555;
+----------+
| COUNT(*) |
|----------|
|        1 |
+----------+