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¶
Required parameters¶
table_nameSpecifies 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 conditionSpecifies 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
USINGclause), it is possible that a row in the target table joins against several rows in theUSINGtable(s). If the DELETE condition is satisfied for any of the joined combinations, the target row is deleted.For example, given tables
tab1andtab2with columns(k number, v number):If you run the following query, the row in
tab1is joined against both rows oftab2:Because at least one joined pair satisfies the condition, the row is deleted. As a result, after the statement completes,
tab1is 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 (
ATorBEFORE), 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:
Load data:
This example shows how to use the WHERE clause to delete a specified row(s). This example deletes by customer_id:
Show the data after the delete:
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.
(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:
Now suppose that another bicycle(s) is returned:
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:
Show the data after the delete:
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:
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:
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:
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:
Undo a delete with ROLLBACK¶
Run the delete in an explicit transaction, check the result, and then roll back to undo it: