DELETE
statementsupportedQueries & DMLRemove 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
supportedpartialnot supportednot scored
DELETE FROM <table_name>
[ USING <additional_table_or_query> [, <additional_table_or_query> ] ]
[ WHERE <condition> ]
Parameters
| Name | Required | Description |
|---|---|---|
table_name | required | Specifies the table from which rows are removed. |
USING additional_table_or_query [, ... ] | optional | 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 | optional | 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): Copy code 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: Copy code 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.