How to Stop an AI Agent From Running DELETE or UPDATE Without a WHERE Clause
A check that the statement contains WHERE is not enough. Parse the SQL, cap the rows, and require approval for bulk or sensitive changes.
Claix · November 2026. Related guides: how to stop an agent from deleting a database, SQL guardrails in 5 minutes, and Slack approvals. Product: Claix Guard.
Why this keeps happening
An agent that can write to a database will, sooner or later, build a statement that is wrong in scope. It misreads an ambiguous instruction, sees an empty result and tries to fix it, or follows text injected into a document it was asked to summarise. The statement looks reasonable and runs with whatever permissions the connection has.
Instructions in the system prompt do not change that. In July 2025 a coding agent from Replit deleted a company's production database during a code freeze, even though it had been told not to make changes without permission. A rule the model can read is a rule the model can reason around. The control has to live outside the model and give the same answer every time.
A WHERE clause is not the same as a bounded change
The most common guardrail is a regular expression that rejects any DELETE or UPDATE that does not contain WHERE. It catches the obvious case and misses most of the dangerous ones:
| Statement | What it does | Caught by a has-WHERE check? |
|---|---|---|
| DELETE FROM orders; | Deletes every row | Yes |
| DELETE FROM orders WHERE 1 = 1; | Deletes every row | No |
| UPDATE orders SET status = 'x' WHERE id IS NOT NULL; | Updates every row | No |
| DELETE FROM orders /* WHERE id = 1 */; | Deletes every row (the word is only in a comment) | No |
| DELETE FROM orders WHERE id = 1; DELETE FROM orders; | Second statement deletes every row | No |
| WITH t AS (SELECT 1) DELETE FROM orders; | Deletes every row | Only if the check expects DELETE at the start |
| DELETE FROM orders WHERE created_at < now(); | Deletes nearly every row | No |
The real question is not whether a WHERE clause exists, but whether the statement is bounded: does it touch a small, intended set of rows? That needs a parser and a rule, not a pattern match.
Layer 1: do not grant what the agent should never do
The cheapest protection is a permission the agent does not have. If the agent never needs to delete, do not grant DELETE. If it only changes one column, grant only that column:
-- The agent can read, insert, and change one column, but never delete
GRANT SELECT, INSERT ON orders TO agent_role;
GRANT UPDATE (status) ON orders TO agent_role;
-- No DELETE privilege is granted, so a DELETE fails at the databaseWhere deletion is a legitimate business need, prefer a soft delete (an UPDATE that sets a flag) so the change is reversible. Permissions are a blunt tool, though: they say whether the agent may run an operation at all, not whether this specific statement should run.
Layer 2: a deterministic check outside the model
Before a write runs, a checker should parse the statement and apply rules. Good rules are structural and do not depend on a model's judgement:
- One statement per check. Reject input that contains more than one statement.
- Only the operations you allow, on the tables you allow. Deny DROP, TRUNCATE and DDL by default.
- A real condition is required on every UPDATE and DELETE, and tautologies such as 1 = 1 or id IS NOT NULL do not count.
- Prefer equality on a key (for example id = $1) for changes the agent proposes, so each statement targets a known row.
- Anything the checker cannot understand is denied. A person cannot review what the tool cannot interpret.
You can build this yourself with a SQL parser. Claix Guard is a hosted version of the idea: you describe your rules in plain English, Claix turns them into exact rules with test cases that you review, and your code sends each proposed statement to /check-sql and receives allow or deny with a reason. No AI model decides at check time, and Claix never executes the statement. Your own code does.
Example rules in plain English:
- Every UPDATE and DELETE must include a condition.
- Agents can never delete orders.
- Discounts cannot exceed 20 percent.
A call looks like this:
curl -X POST https://claix.dev/check-sql \
-H 'x-api-key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"sql": "DELETE FROM orders"}'A denied response has this shape (values are placeholders):
{
"allowed": false,
"decision_id": "<uuid>",
"code": "<code>",
"reason": "<why the statement was denied>",
"operation": "delete",
"table": "orders",
"violations": [{ "rule_title": "<rule>", "message": "<message>" }],
"latency_ms": 0
}Because the reason is written in plain language, the agent can read it and propose a narrower statement instead of failing with a cryptic database error.
Layer 3: a row cap in your executor
Even a statement that passes the structural rules can touch more rows than intended: WHERE status = 'pending' may match ten rows today and ten million next year. The simplest safeguard is to run the write inside a transaction and roll it back if it affects more rows than you allow. This code belongs in the executor you control, after the check has returned allow:
import psycopg
MAX_ROWS = 5
def run_checked_write(conn: psycopg.Connection, sql: str, params: tuple):
# Run only after your deterministic check returned allow.
with conn.transaction(): # rolls back if an exception is raised
cur = conn.execute(sql, params)
if cur.rowcount > MAX_ROWS:
raise RuntimeError('Row cap exceeded; change rolled back')
return cur.rowcountOne caveat: the database does the work before it rolls back, and it holds locks while it does. For very large tables, count the matching rows first with a SELECT that uses the same condition, and set a statement_timeout for the agent's role.
Layer 4: human approval for bulk or sensitive changes
Some changes are legitimate but deserve a second look: a refund above a threshold, a change to a sensitive table, anything larger than usual. For those, use a third outcome besides allow and deny: needs approval. Claix Guard supports a Needs approval verdict that posts the proposed change to Slack with Approve and Reject buttons, which call the endpoints you configure. Your own endpoint is what runs the statement after someone approves. Setup: connect Slack.
What each layer stops
| Approach | No WHERE at all | Tautologies like 1 = 1 | Hidden second statement | Large but bounded changes |
|---|---|---|---|---|
| Instruction in the prompt | No | No | No | No |
| Regex for WHERE | Yes | No | No | No |
| Role without the privilege | Yes, for anything not granted | Yes, for anything not granted | Yes, for anything not granted | Yes, for anything not granted |
| Deterministic check (parser and rules) | Yes | Yes, if you write the rule | Yes | Partly, with key-equality rules |
| Row cap in a transaction | Yes | Yes | Only for the statement it wraps | Yes |
| Human approval | Yes | Yes | Yes | Yes |
No single layer is enough. Permissions limit what is possible, the check limits what is allowed, the cap limits the damage if something slips through, and approval handles judgement calls.
Frequently asked questions
- How do I stop an AI agent from deleting all rows in a table?
- Do not grant DELETE unless it is needed, parse every proposed statement with a deterministic checker that denies unbounded DELETE and UPDATE, cap the rows a change may affect inside a transaction, and require human approval for bulk or sensitive changes.
- Is checking for a WHERE clause enough?
- No. Statements such as WHERE 1 = 1, WHERE id IS NOT NULL, a WHERE inside a comment, or a second statement after a semicolon all affect every row while passing a simple has-WHERE check. You need a parser and rules about what makes a condition bounded.
- Why not just tell the agent to always include a WHERE clause?
- A prompt is a request to the model, not a restriction. Misreadings and prompt injection can lead the model to ignore it. Enforce the rule outside the model.
- Can I allow single-row updates but block bulk updates?
- Yes. Require an equality condition on a key for agent-proposed changes, and add a row cap in your executor so a statement that unexpectedly matches many rows is rolled back.
- Does a database transaction protect me?
- It lets you undo a change before it is committed, which makes a row cap possible. It does not stop the agent from generating a destructive statement, and the database does the work and holds locks until the rollback.
- Should I write the SQL parser myself?
- You can, with an existing SQL parser library, but the edge cases (comments, CTEs, multiple statements, engine differences) take time to get right and to keep right. A hosted checker such as Claix Guard moves that work and the rule management out of your codebase.
- What happens when Claix Guard denies a statement?
- It returns allowed false with a reason and the violated rules. Your code does not run the statement, and the agent can use the reason to propose a different one.
- Does Claix run the SQL?
- No. Claix never executes your statements and has no write access to your database. It only returns the verdict. Your code or workflow executes the statement when it is allowed.
- What about DROP and TRUNCATE?
- Deny them by default for agent connections, and handle schema changes through a separate, reviewed process.
Conclusion
An unbounded DELETE or UPDATE is one of the most damaging things an agent can do, and the usual protections, a prompt rule or a regex for WHERE, fail against simple variations. Layer your defences: fewer permissions, a deterministic check outside the model, a row cap in a transaction, and human approval where judgement is needed. If you would rather not maintain the checker yourself, Claix Guard lets you write the rules in plain English and returns an allow or deny with a reason on every statement. Try Claix Guard free: 30 successful calls per month.
You might also like…
Security · Guardrails
How to stop an AI agent from deleting your database: guardrails and rules (2026)
Guide · Guard
Set up your agent's first SQL guardrails in 5 minutes
Product · Guard
Claix Guard Now Sends Alerts and Approval Requests to Slack
Security · Guardrails
Prompt Injection Database Data Leak: How to Limit the Damage From AI Agents