DELETE SQL statement.
Before you begin
Before reading this page, do the following:- Create a CockroachDB Standard cluster or start a local cluster.
- Install a Driver or ORM Framework.
- Connect to the database.
- Create a database schema.
-
Insert data that you now want to delete.
In the examples on this page, we use sample
movrdata imported with thecockroach workloadcommand.
Use DELETE
To delete rows in a table, use a DELETE statement with a WHERE clause that filters on the columns that identify the rows that you want to delete.
SQL syntax
In SQL,DELETE statements generally take the following form:
- is a table with rows that you want to delete.
- is the column to filter on.
- is a comparison operator that resolves to
TRUEorFALSE(e.g.,=). - is the matching value for the filter.
Best practices
Here are some best practices to follow when deleting rows:- Limit the number of
DELETEstatements that you execute. It’s more efficient to delete multiple rows with a single statement than to execute multipleDELETEstatements that each delete a single row. - Always specify a
WHEREclause inDELETEqueries. If noWHEREclause is specified, CockroachDB will delete all of the rows in the specified table. - To delete all of the rows in a table, use a
TRUNCATEstatement instead of aDELETEstatement. - To delete a large number of rows (i.e., tens of thousands of rows or more), use a batch-delete loop.
- When executing
DELETEstatements from an application, make sure that you wrap the SQL-executing functions in a retry loop that handles transaction errors that can occur under contention. - Review the performance considerations below.
Examples
Delete rows filtered on a non-unique column
Suppose that you want to delete the vehicle location history data recorded during a specific hour of a specific day. To delete all of the rows in thevehicle_location_histories table where the timestamp is between two TIMESTAMP values:
cockroach sql reference docs.
Delete rows filtered on a unique column
Suppose that you want to delete the promo code data for a specific set of codes. To delete the rows in thepromo_codes table where the code matches a string in a set of string values:
cockroach sql reference docs.
Performance considerations
Because of the way CockroachDB works under the hood, deleting data from the database does not immediately reduce disk usage. Instead, records are marked as “deleted” and processed asynchronously by a background garbage collection process. Once the marked records are older than the specified TTL interval, they are eligible to be removed. The garbage collection interval is designed to allow sufficient time for running backups and time travel queries usingAS OF SYSTEM TIME. The garbage collection interval is controlled by the gc.ttlseconds setting.
The practical implications of the above are:
- Deleting data will not immediately decrease disk usage.
- If you issue multiple
DELETEstatements in sequence that each delete large amounts of data, each subsequentDELETEstatement will run more slowly. For details, see PreservingDELETEperformance over time.
See also
Reference information related to this task:DELETE- Bulk-delete data
- Batch Delete Expired Data with Row-Level TTL
- Disk space usage after deletes
TRUNCATEDROP TABLE- Transaction Contention

