How to stop a Cloudflare Worker fast without deleting it
Deleting a Worker with Durable Objects can delete their data. Deploy a stub to stop new work and keep the storage. Rollback restores the code, not consumed alarms or terminated Workflows.
When a Worker is on fire your first instinct is to delete it. Please don't, at least not if it has Durable Objects. Deleting the class deletes the data.
What I do instead is deploy a stub version. Workers keep version history, so you upload a tiny version that does nothing, deploy it, and roll back when the fire is out.
export default {
fetch: () => new Response("paused", { status: 503 }),
scheduled: () => {},
// An empty queue handler acks the batch and drops the messages. Retry instead.
queue: (batch) => batch.retryAll({ delaySeconds: 43200 }),
};
// Not a complete DO stub: keep the same Durable Object class exports and
// wrangler config, with no-op handlers, or the deploy fails.Then, when you've fixed things:
npx wrangler rollback <previous-version-id> --name <script>If it has Durable Objects, busy objects can keep running the old code for up to five minutes after a deploy (by default). In an emergency, pass --durable-objects-code-update-mode immediate to the deploy. It resets active objects right away, so in-flight work gets cut off.
Gotchas
- The stub must export the same Durable Object classes or the deploy fails.
- DO alarms that fire while paused are consumed. They don't come back on rollback.
- Queue messages get retried while paused, and those retries count against
max_retries. Once a message runs out, it goes to the dead letter queue if you have one, otherwise it's deleted. Messages can also expire during a long pause. - A stub deploy doesn't stop live Workflow instances. They keep running the old code, so you have to terminate them, and a rollback won't start them again.
This is what workerfuse does when a Worker passes its limit, and the alert email includes the rollback command.