rollback-count

The rollback-count command sequentially reverts a specified number of changesets on your database.

Uses

Use the rollback-count command when you want to roll back changes sequentially, starting with the most recent changes and working backward until the count you specify is reached.

The image below shows three changesets: a, b, and c. Running the command rollback-count --count=3 rolls back the last three changesets.

If you want to revert changeset a without having to roll back changeset b and changeset c as well, you can do it with the help of the rollback-one-changeset command.

Impact

Using rollback-count comes with the risk of removing all changes depending on the number you specify. Also, when you roll back any change, the row associated with that change is deleted from the DATABASECHANGELOG table.

Warning: rollback-count increments based on changesets in the changelog, not records in the DATABASECHANGELOG table. If you manually delete changesets from your changelog, rollback-count does not restore those changes. For best practices on maintaining large changelogs, see Trimming changelog files.

Look for unintended consequences before using this command.

The best practice is to run the rollback-count-sql command helper command because it allows you to inspect the rollback-count SQL and search for any potential mistakes before you execute the rollback-count command.

Syntax

Before running the rollback-count command, you can run the history command to see all your previously executed changes.

Note: rollback-count works from the bottom of the list toward the top.

To run the rollback-count 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 in your command line.

Then run the rollback-count command:

liquibase rollback-count --count=2 --changelog-file=example-changelog.xml

Note: The --count=myCount syntax was added in Liquibase 4.4. If you use an older version, specify your count as a positional argument: <command> myCount.

Command arguments

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
--count

Integer specifying how many changes Liquibase applies the command to. Specify as --count=myCount. Positional format <command> <count> deprecated in 4.4+.

Required
--url

The JDBC database connection URL. See Using JDBC URL in Liquibase.

Required
--change-exec-listener-class

Fully-qualified class which specifies a ChangeExecListener. For more information, see Implementing a Custom ChangeExecListener Class with Liquibase and ChangeExecListenerCommandStep.

Optional
--change-exec-listener-properties-file

Path to a properties file for the ChangeExecListener class. For more information, see Implementing a Custom ChangeExecListener Class with Liquibase and ChangeExecListenerCommandStep.

Optional
--context-filter

Specifies the changeset contexts to match. Contexts are tags you can add to changesets to control which changesets are executed in any particular migration run.

Note: If you use Liquibase 4.23.0 or earlier, use the syntax --contexts instead of --context-filter.

Optional
--default-catalog-name

Name of the default catalog to use for the database connection

Optional
--default-schema-name

Name of the default schema to use for the database connection. If defaultSchemaName is set, then objects do not have to be fully qualified. This means you can refer to just mytable instead of myschema.mytable.

Tip: In Liquibase v4.23.0+, camelCase for defaultSchemaName works successfully. If you are on an earlier version, camelCase may not work as expected.

Note: The syntax liquibase.command.defaultSchemaName is valid for v4.19.0+. For prior versions, use defaultSchemaName.

Optional
--driver

The JDBC driver class

Optional
--driver-properties-file

The JDBC driver properties file

Optional
--label-filter

Specifies the changeset labels to match. Labels are tags you can add to changesets to control which changesets will be executed in any migration run.

Optional
--password

Password to connect to the target database.

Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro.

Optional
--rollback-script

The path to the script to use to perform the rollback. Only needed if the rollback is not already defined in the changelog, and if it is not a rollback statement that is automatically generated by Liquibase.

Optional
--username

Username to connect to the target database.

Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro.

Optional

* Liquibase will check nested changelogs for definitions of the changesets to rollback

Note: The username and password attributes are not required for connections and systems which use alternate means of authentication. Also, you can specify database credentials as part of the url attribute.