rollback
Undo already-applied migrations by executing the down() function from each migration file. By default, rollback reverts the most recent batch — every migration that was applied together in the last migrate run.
Synopsis
bash
bunx d1-eloquent rollback [--step=<n>] [--batch=<n>] [--pretend] [--atomic] [--db=<DB_NAME>] [--remote]Options
| Flag | Description |
|---|---|
--step=<n> | Roll back the n most recently applied migrations, regardless of batch. Must be written as --step=3 (the --step 3 space form is not parsed). |
--batch=<n> | Roll back every migration recorded under a specific batch number. |
--pretend | Print the down() SQL that would run for each targeted migration, without executing it. |
--atomic | Wrap each migration's rollback (its down() SQL plus the _migrations delete) in a single transaction. Applies to remote execution only. |
--db=<DB_NAME> | Optional. The D1 binding name. Auto-detected from wrangler.jsonc (also wrangler.json / wrangler.toml), falls back to 'DB'. |
--local | Run against the local D1 database. This is the default. |
--remote | Run against the Cloudflare-hosted D1 database. |
With neither --step nor --batch, rollback targets the entire latest batch.
Examples
Roll back the most recent batch:
bash
bunx d1-eloquent rollbackRoll back the three most recently applied migrations:
bash
bunx d1-eloquent rollback --step=3Roll back a specific batch:
bash
bunx d1-eloquent rollback --batch=2Preview the down() SQL without executing it:
bash
bunx d1-eloquent rollback --pretendRoll back the latest batch on the remote database:
bash
bunx d1-eloquent rollback --remoteNotes
- Targeted migrations are rolled back newest-first, so each
down()runs in the reverse of the order itsup()was applied. - Ensure your
down()function correctly reverses every change made byup(). Ifdown()is missing or incomplete, rollback cannot fully restore the prior schema. - After each migration is rolled back, its record is removed from
_migrationsso it can be re-applied withmigrate. - If there are no applied migrations,
rollbackprintsNo migrations to rollback.and exits without changes.