Overview
These rules flagUPDATE and DELETE statements that lack a WHERE clause. Without a WHERE clause, these statements affect every row in the table, which is rarely intended and can cause catastrophic data loss.
Rule names:
update-without-wheredelete-without-where
When These Rules Trigger
update-without-where
This rule triggers when:- An
UPDATEstatement has noWHEREclause
UPDATEhas aWHEREclauseUPDATEusesFROMwith aWHEREclause for joins
delete-without-where
This rule triggers when:- A
DELETEstatement has noWHEREclause
DELETEhas aWHEREclauseDELETEusesUSINGwith aWHEREclause for joins
Why It Matters
Data Loss Risk
Without aWHERE clause:
UPDATEmodifies every single row in the tableDELETEremoves every single row from the table
Common Mistakes
- Forgetting the WHERE clause: Most common during development or in SQL clients
- Wrong statement order: Writing
UPDATE table SET col = valand forgetting to addWHEREbefore executing - Copy-paste errors: Copying a statement and forgetting to update the condition
When It’s Intentional
Sometimes you genuinely want to update/delete all rows:- Resetting a staging table
- Bulk status updates
- Clearing test data
Examples
Problematic: UPDATE without WHERE
Problematic: UPDATE without WHERE
Problematic: DELETE without WHERE
Problematic: DELETE without WHERE
Good: UPDATE with WHERE
Good: UPDATE with WHERE
id = 1.Good: DELETE with WHERE
Good: DELETE with WHERE
id = 1.Good: UPDATE with FROM and WHERE
Good: UPDATE with FROM and WHERE
Good: DELETE with USING and WHERE
Good: DELETE with USING and WHERE
Intentional: Explicit WHERE TRUE
Intentional: Explicit WHERE TRUE
WHERE TRUE explicitly signals “yes, I mean all rows.”Intentional: Use TRUNCATE instead of DELETE
Intentional: Use TRUNCATE instead of DELETE
TRUNCATE is faster and clearer in intent.Best Practices
Always Use WHERE
Make it a habit to writeWHERE before executing UPDATE or DELETE:
Test with SELECT First
Before runningUPDATE or DELETE, test your WHERE clause with SELECT:
Use Transactions
For critical operations, use transactions so you can rollback if something goes wrong:For Intentional Bulk Operations
If you genuinely need to affect all rows:Implementation Details
update-without-where
The rule works by:- Checking if the statement is an
UpdateStmt - Checking if
WhereClauseisnil - Reporting a warning at the start of the statement
update_without_where.go
delete-without-where
The rule works by:- Checking if the statement is a
DeleteStmt - Checking if
WhereClauseisnil - Reporting a warning at the start of the statement
delete_without_where.go