rollback

The rollback command rolls back changes made to the database based on the specified tag.

Uses

The rollback command is typically used to revert all changes that were made to the database after the tag you specify.

When you run rollback, Liquibase will roll back sequentially all the deployed changes until it reaches the tag row in the DATABASECHANGELOG table. For example, you can use the rollback command when you want to undo a series of changes made to your database related to a specific tag such as a numbered release. If you have tags for release 1, release 2, and release 3, and need to make a correction in release 2, the rollback command will rollback release 3 first.

The following image shows that if we deploy the createTable C changeset and run the rollback version1 command to revert changes, Liquibase will roll back only createTable C value:

Impact of running the rollback command

Using the rollback command comes with risks to your database, so it's important to look for potential unintended consequences before executing this command. You can do this with the rollback-sql command.

Running the rollback command

To run the rollback command, specify the driver, classpath, and URL in the Liquibase properties file. For more information, see Create and Configure a liquibase.properties File. You can also specify these properties in your command line.

Also, before running the rollback command, you need to know the following:

  • If the tag name is unknown to you, you can find it in the DATABASECHANGELOG table.
  • If you don’t have any tags specified, you can run the tag command. If you run the tag command to mark the current database state or release, your tag will be applied to the last row in the DATABASECHANGELOG table.
  • If you use the tagDatabase Change Type to create a tag changeset in the changelog file and want to roll back changes applied after this tag, the rollback command will remove all changes made after this tag row and the tag row.
  • If you run the tag command, deploy changesets, and then add the tagDatabase Change Type in your changelog file, your changes and the tag row created by the tagDatabase Change Type will be removed till the command reaches the tag specified with the rollback command.
liquibase --changelog-file=dbchangelog.xml rollback version1

Note: Enter the name of your changelog and tag in place of dbchangelog.xml and version1.

rollback global attributes

Tip: For best results, specify all commands and parameters in the --kebab-case format in the CLI. If your preference is camelCase, it also works in the CLI.

Attribute Definition Requirement
--changelog-file

The root changelog

Required
--url

The JDBC database connection URL

Required
--username*

The database username

Required
--password*

The database password

Required

Note: The username and password attributes are not required for connections and systems which use alternate means of authentication.

rollback command attributes

Attribute Definition Requirement
<tag>

The tag identifying which tagged changesets in the changelog to evaluate.

Required

Additional rollback functionality

  • Add custom rollback SQL to the changelog for any changesets which Liquibase cannot auto-generate rollback SQL. Liquibase cannot auto-generate rollbacks for SQL changelogs. Also, some Change Types have no corresponding rollback commands that can be automatically generated.
    -- changeset liquibaseuser:1
    create table Details1 ( id int primary key, name varchar(255) );
    -- rollback drop table Details1;
  • If you do not want to revert a change in a rollback mode, use the empty keyword (SQL) or a self-closing tag (XML):
    -- changeset liquibaseuser:1
    create table example1 ( id int primary key, name varchar(255) );
    -- rollback empty
    <changeSet id="noRollback" author="liquibase">
      <createTable tableName="changeRollback">
        <column name="id" type="int"/>
      </createTable>
    <rollback/>
    </changeSet>
  • Specify the rollback commands using the tag within the changeset tag when you want to override the default generated rollback commands. You can also specify multiple rollback.
    <changeSet  author="liquibase"  id="30">
      <createTable tableName="table30">
        <column name="id" type="int"/>
        <column name="heading" type="varchar(36)"/>
        <column name="author" type="varchar(36)"/>
      </createTable>
    <rollback>
    drop table table30
    </rollback>
    </changeSet>
    
    <changeSet id="multiRollbackTest" author="rs">
      <createTable tableName="multiRollback1">
        <column name="id" type="int"/>
      </createTable>
      <createTable tableName="multiRollback2">
        <column name="id" type="int"/>
      </createTable>
      <createTable tableName="multiRollback3">
        <column name="id" type="int"/>
      </createTable>
    <rollback>
    drop table multiRollback1;
    drop table multiRollback2;
    </rollback>
    <rollback>drop table multiRollback3</rollback>
    </changeSet>
  • Use the rollback to reference the changeset that originally created a statement.
    <changeSet id="changeRollback2-drop" author="liquibase">
      <dropTable tableName="changeRollback2"/>
      <rollback changeSetId="changeRollback2-create" changeSetAuthor="liquibase"/>
    </changeSet>