knex migrate:rollback: Undo the Last Batch
knex migrate:rollback reverts the most recent batch of migrations by running their down() functions.
Knex groups migrations run together into a batch. rollback undoes the last batch; --all undoes every batch.
What it does
knex migrate:rollback runs the down() function of each migration in the latest batch, in reverse order, and removes their rows from knex_migrations. With --all it rolls back every batch down to an empty schema.
Common usage
# revert the last batch
npx knex migrate:rollback --env production
# revert all batches
npx knex migrate:rollback --all --env productionOptions
| Flag | What it does |
|---|---|
| --all | Roll back every batch, not just the last |
| --env <name> | Environment block to use |
| --knexfile <path> | Path to the knexfile |
In CI
rollback only works if each migration has a real down(); Knex migrations with an empty or missing down() roll back nothing. Because rollback works by batch, a partial deploy can span batches in ways that surprise you, so prefer forward fixes in production and use rollback on ephemeral test databases.
Common errors in CI
"Migration table is already locked" means a stuck knex_migrations_lock; run knex migrate:unlock. "TypeError: ... down is not a function" means a migration lacks a down() export. If rollback reverts fewer migrations than expected, they were applied in separate batches, so run rollback again or use --all.