Java.DBUpdate.How can you rollback only the last changeset?

In Liquibase, you roll back only the last executed changeset using a count-based rollback.


The direct answer (what interviewers want)

liquibase rollbackCount 1

This rolls back exactly the most recently applied changeset, using its explicit rollback definition (if present).

How this actually works (important)

  1. Liquibase looks at DATABASECHANGELOG
  2. Finds the last executed changeset
  3. Executes its <rollback> block
  4. Marks that changeset as rolled back

If the changeset does not define a rollback:

  • Liquibase will fail
  • or try an automatic rollback (very limited, often unsafe)

Example changeset with rollback

<changeSet id="add-status" author="stanley">
    <addColumn tableName="orders">
        <column name="status" type="varchar(20)"/>
    </addColumn>

    <rollback>
        <dropColumn tableName="orders" columnName="status"/>
    </rollback>
</changeSet>

Command:

liquibase rollbackCount 1

Alternative rollback strategies (know these)

Roll back to a tag

liquibase rollback release_1_2

Roll back to a date

liquibase rollbackToDate 2026-01-10T12:00:00

These are often safer than rollbackCount in real systems.

Critical production warning 🚨

Rolling back schema in production is dangerous once data has changed.

Senior teams usually prefer:

  • forward rollbacks (new compensating migration)
  • rollback commands only in dev / test / controlled releases

Interview-ready answer (2 sentences)

You can roll back the last changeset in Liquibase using rollbackCount 1, which executes the explicit rollback defined for the most recently applied changeset. In production, this is used sparingly, since rolling back schema after data changes is risky and teams usually fix forward instead.

Senior rule of thumb

If you’re afraid to read the rollback SQL, don’t run the rollback.

This entry was posted in Без рубрики. Bookmark the permalink.