Repository navigation
Description
Summary
DELETE /v1/webhooks/:webhookId deletes unconditionally. A client that decides to delete a webhook based on what it read earlier cannot make the delete depend on that read, so a concurrent update between the read and the delete is lost.
Appwrite already has a precondition for this on documents, rows and account writes: the X-Appwrite-Timestamp header. Webhook delete and update should honour it too.
Use case
The Appwrite MCP server (appwrite/mcp#127) implements MCP Events by managing one Appwrite webhook per subscription in the user's project. The server is stateless: the webhook is the only record of the subscription, and its name carries the lease expiry.
On each events/subscribe, the server cleans up the caller's webhooks whose lease expired more than 10 minutes ago, so stale subscriptions don't hold Free-plan webhook slots. Cleanup lists the webhooks, re-reads each candidate, and deletes it if its name and $updatedAt are unchanged.
The window between that re-read and the DELETE can't be closed from the client:
cleanup (request A) refresh (request B, same subscription)
GET /v1/webhooks/{id} unchanged
PUT /v1/webhooks/{id} new lease, 200
subscribe returns success to the client
DELETE /v1/webhooks/{id} -> deleted, refresh lost
Client B was told its subscription is live, but its webhook is gone, so events are dropped until its next refresh. It's rare, but every client doing read-then-delete on webhooks has this gap. The mcp-event-subscriptions Appwrite profile asks for "conditional deletion or equivalent coordination" for this reason.
Proposal
Honour the existing X-Appwrite-Timestamp header on webhook delete and update, with the same semantics it has on documents and rows:
DELETE /v1/webhooks/{webhookId}
X-Appwrite-Timestamp: 2026-10-12T06:36:34.123+00:00
- Header absent: behaves as today.
- Stored
$updatedAtis not later than the header: deletes and returns204. - Stored
$updatedAtis later than the header: returns409with a newwebhook_update_conflicterror, matchingdocument_update_conflictandrow_update_conflict, and nothing is deleted. - Webhook missing: returns
404 webhook_not_foundas today.
The client sends the $updatedAt it read, so the check doesn't depend on clocks: any write after the read sets a later $updatedAt. That includes the delivery worker's writes (attempts, logs, auto-pause on failures), so a 409 can also mean a delivery failed since the read; read-then-delete clients treat it as "changed, re-read". The same header on PUT /v1/webhooks/{webhookId} (and the secret update) prevents lost updates between concurrent writes.
Implementation notes
Database::deleteDocument and updateDocument already do the check atomically: they read the row with forUpdate: true inside a transaction and throw ConflictException when $updatedAt is later than the request timestamp. The endpoint only needs to set it, as Databases/Collections/Documents/Delete.php does:
try {
$dbForPlatform->withRequestTimestamp($requestTimestamp, fn () => $authorization->skip(
fn () => $dbForPlatform->deleteDocument('webhooks', $webhook->getId())
));
} catch (ConflictException) {
throw new Exception(Exception::WEBHOOK_UPDATE_CONFLICT);
}Comparing $updatedAt in PHP between findOne and deleteDocument would leave the race in place server-side, so the check has to stay inside the database call.
Cover it by extending the webhook e2e tests in tests/e2e/Services/Webhooks/WebhooksBase.php: delete with the current $updatedAt succeeds; delete after an intervening update returns 409 and the webhook still exists.
Why not a new header, If-Match or an SDK param
- A new
X-Appwrite-If-Updated-Atheader with412would duplicateX-Appwrite-Timestampwith a different status code on one endpoint. If-Matchneeds ETags, which Appwrite doesn't return for platform resources.If-Unmodified-Sinceonly has second precision;$updatedAthas milliseconds.X-Appwrite-Timestampisn't a parameter in the SDKs on any endpoint today (SDK users set it as a client header). Exposing preconditions as SDK parameters should be one decision for every endpoint that honours the header, not a webhook-only param.
Related
- appwrite/mcp#127: MCP Events
- appwrite/mcp#135: the cleanup race as reported in review
- Encrypted and length-limited attributes return 500 instead of 400 (webhooks authPassword/authUsername/url, variables value) #14293: the other blocker for the same feature (
authPasswordstorage)
Activity
SahilArate commentedon Oct 25, 2026
Hi! I’d like to implement this. Before I start, one design question: should the precondition be the X-Appwrite-If-Updated-At header only, or also an optional ifUpdatedAt param so it appears in the SDKs and specs? My plan is to do the check inside withTransaction with a forUpdate read in Webhooks/Delete.php, return a new 412 error, and add e2e tests for the match, mismatch, missing and invalid-date cases. @ChiragAgg5k, if you are planning to build this yourself, I’m happy to leave it with you. Could this be assigned to me?
Jishnu09-siuu commentedon Nov 28, 2026
Hi! I'd like to work on this issue and can implement the conditional delete inside withTransaction using forUpdate: true in Webhooks/Delete.php along with the 412 error and E2E tests.
Could you please assign this to me and confirm whether you prefer the X-Appwrite-If-Updated-At header only, or also an optional ifUpdatedAt parameter so it is exposed in the SDKs?
[-]Conditional delete for webhooks (delete only if unchanged since read)[/-][+]Honour X-Appwrite-Timestamp on webhook delete and update[/+]ChiragAgg5k commentedon Nov 28, 2026
@SahilArate @Jishnu09-siuu thanks for offering. On the design question: neither a new header nor an
ifUpdatedAtparam. Appwrite already supportsX-Appwrite-Timestampas a precondition on documents and rows, so I've updated the proposal to reuse it on webhook delete and update, returning 409webhook_update_conflict.#15450 is already open for this, so I've left review notes there to move it to that approach.
sunztar commentedon Nov 29, 2026
@SahilArate @Jishnu09-siuu thanks for offering. On the design question: neither a new header nor an ifUpdatedAt param. Appwrite already supports X-Appwrite-Timestamp as a precondition on documents and rows, so I've updated the proposal to reuse it on webhook delete and update, returning 409 webhook_update_conflict.