DELETE statement deletes rows from a table.
To delete columns, see
ALTER TABLE ... DROP COLUMN.Required privileges
You must have theDELETE and SELECT privileges on the table.
Syntax
Parameters
See Common Table Expressions.
The name of the table that contains the rows you want to delete.
An alias for the table name. When an alias is provided, it completely hides the actual table name.
Delete rows based on a table join, where
table_ref specifies another table or tables to reference.A condition that returns Boolean values using columns. Delete rows that return
TRUE.Without a WHERE clause, DELETE removes all rows from the table. To delete all rows in a table, we recommend using TRUNCATE instead of DELETE.An
ORDER BY clause for ordering query results.A
LIMIT clause to limit the number of rows deleted.Return values based on rows deleted, where
target_list can be specific column names from the table, * for all columns, or computations using scalar expressions.To return nothing in the response, not even the number of rows deleted, use RETURNING NOTHING.Success responses
SuccessfulDELETE statements return one of the following:
DELETEint - int rows were deleted.DELETEstatements that do not delete any rows respond withDELETE 0. WhenRETURNING NOTHINGis used, this information is not included in the response.- Retrieved table - Including the
RETURNINGclause retrieves the deleted rows, using the columns identified by the clause’s parameters.
Disk space usage
Deleting a row does not immediately free up the disk space. CockroachDB retains the ability to query tables historically. If disk usage is a concern, reduce the time-to-live (TTL) for the zone by settinggc.ttlseconds to a lower value, which will cause garbage collection to clean up deleted objects more frequently.
Force index selection
By using the explicit index annotation (index hinting), you can override CockroachDB’s index selection and use a specific index for deleting rows.Examples
Delete rows using primary key/unique columns
Using columns with the Primary Key or Unique constraints to delete rows ensures your statement is unambiguous—no two rows contain the same column value.Delete rows using non-unique columns
Deleting rows using non-unique columns removes every row that returnsTRUE for the WHERE clause.
Delete rows using a table join
You can delete rows based on a table join. Use theUSING clause to specify another table.
Return deleted rows
To see which rows your statement deleted, include theRETURNING clause to retrieve them using the columns you specify.
Use all columns
By specifying*, you retrieve all columns of the deleted rows:
Use specific columns
To retrieve specific columns, name them in theRETURNING clause:
Change column labels
WhenRETURNING specific columns, you can change their labels using AS:
Sort and return deleted rows
To sort and return deleted rows:Delete with index hints
Suppose you create a multi-column index on theusers table:
See also
- INSERT
- UPDATE
- UPSERT
- SELECT
- TRUNCATE