rollback <tag> command

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

Uses

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

When you run rollback <tag>, 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 <tag> 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 <tag> 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 <tag> command

Using the rollback <tag> command comes with risks to your database, so it's important to look for potential unintended consequences before executing this command.

It is a best practice to run the rollbackSQL <tag> command that allows you to inspect for potential mistakes before running the command.

Running the rollback <tag> command

To run the rollback <tag> command, you need to specify your driver, class path, and URL in your liquibase.properties file. For more information, see Creating and configuring a liquibase.properties file. You can also specify these properties in your command line.

Also, before running the rollback <tag> 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 <tag string> 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 <tag> 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 <tag> command.
liquibase --changeLogFile=dbchangelog.xml rollback version1

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

rollback <tag> global attributes

Attribute Definition Requirement
--changeLogFile The root changelog Required
--url The JDBC database connection URL Required
--username The database username Required
--password The database password Required

rollback <tag> command attributes

Attribute Definition Requirement
<tag> The tag you can add to changesets to determine which changesets in the changelog to evaluate based on their tags. Required

Additional rollback <tag> 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;
  • Use an empty tag when you do not want to undo changes in a rollback mode.
<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 <tag>.
<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 <tag> 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>