> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ROLLBACK

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `ROLLBACK` <InternalLink path="sql-statements">statement</InternalLink> aborts the current <InternalLink path="transactions">transaction</InternalLink> and all of its <InternalLink path="transactions#nested-transactions">nested transactions</InternalLink>, discarding all transactional updates made by statements included in the transaction.

There are two ways to use `ROLLBACK`:

* The `ROLLBACK` statement [rolls back the entire transaction](#rollback-a-transaction).
* The `ROLLBACK TO SAVEPOINT` statement [rolls back and restarts the nested transaction](#rollback-a-nested-transaction) started at the corresponding `SAVEPOINT` statement, for working with <InternalLink path="savepoint#savepoints-for-nested-transactions">standard savepoints</InternalLink>. This is in addition to the existing support for working with <InternalLink path="transaction-retry-error-reference#client-side-retry-handling">client-side transaction retries</InternalLink>. For examples of each usage, see:
  * [Rollback a nested transaction](#rollback-a-nested-transaction)
  * [Retry a transaction](#retry-a-transaction)

<Danger>
  Rollbacks to savepoints over [DDL](https://en.wikipedia.org/wiki/Data_definition_language) statements are only supported if you're rolling back to a savepoint created at the beginning of the transaction.
</Danger>

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/kYb5CU8_1XGPxWhU/images/sql-diagrams/v24.1/rollback_transaction.svg?fit=max&auto=format&n=kYb5CU8_1XGPxWhU&q=85&s=0159bf0de14fe23ec75a93cfb0eba756" alt="rollback_transaction syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="515" height="69" data-path="images/sql-diagrams/v24.1/rollback_transaction.svg" />

## Required privileges

No <InternalLink path="security-reference/authorization#managing-privileges">privileges</InternalLink> are required to rollback a transaction. However, privileges are required for each statement within a transaction.

## Parameters

| Parameter                         | Description                                                                                                                                                                                                                                                                          |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `TO SAVEPOINT cockroach\_restart` | If using <InternalLink path="advanced-client-side-transaction-retries">advanced client-side transaction retries</InternalLink>, retry the transaction. You should execute this statement when a transaction returns a `40001` / `retry transaction` error.                           |
| `TO SAVEPOINT `                   | If using <InternalLink path="savepoint#savepoints-for-nested-transactions">nested transactions</InternalLink>, roll back and restart the <InternalLink path="transactions#nested-transactions">nested transaction</InternalLink> started at the corresponding `SAVEPOINT` statement. |

## Savepoints and row locks

CockroachDB supports exclusive row locks.

* In PostgreSQL, row locks are released/cancelled upon <InternalLink path="rollback-transaction">`ROLLBACK TO SAVEPOINT`</InternalLink>.
* In CockroachDB, row locks are preserved upon <InternalLink path="rollback-transaction">`ROLLBACK TO SAVEPOINT`</InternalLink>.

This is an architectural difference that may or may not be lifted in a later CockroachDB version.

The code of client applications that rely on row locks must be reviewed and possibly modified to account for this difference. In particular, if an application is relying on <InternalLink path="rollback-transaction">`ROLLBACK TO SAVEPOINT`</InternalLink> to release row locks and allow a concurrent transaction touching the same rows to proceed, this behavior will not work with CockroachDB.

## Savepoints and high priority transactions

<InternalLink path="rollback-transaction#rollback-a-nested-transaction">`ROLLBACK TO SAVEPOINT`</InternalLink> (for either regular savepoints or "restart savepoints" defined with `cockroach_restart`) causes a "feature not supported" error after a DDL statement in a <InternalLink path="transactions#transaction-priorities">`HIGH PRIORITY` transaction</InternalLink>, in order to avoid a transaction deadlock.

## Examples

### Rollback a transaction

Typically, an application conditionally executes rollbacks, but we can see their behavior by using `ROLLBACK` instead of `COMMIT` directly through SQL:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM accounts;
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> BEGIN;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> UPDATE accounts SET balance = 2500 WHERE name = 'Marciela';
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> ROLLBACK;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM accounts;
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
+----------+---------+
|   name   | balance |
+----------+---------+
| Marciela |    1000 |
+----------+---------+
```

### Rollback a nested transaction

The `ROLLBACK TO SAVEPOINT` statement rolls back and restarts the <InternalLink path="transactions#nested-transactions">nested transaction</InternalLink> started at the corresponding `SAVEPOINT` statement.

For examples showing how to use `ROLLBACK TO SAVEPOINT` to rollback a nested transaction, see <InternalLink path="savepoint#savepoints-for-nested-transactions">the `SAVEPOINT` documentation on nested savepoints</InternalLink>.

### Retry a transaction

When using <InternalLink path="advanced-client-side-transaction-retries">advanced client-side transaction retries</InternalLink>, use `ROLLBACK TO SAVEPOINT` to handle a transaction that needs to be retried (identified via the `40001` error code or `restart transaction` string in the error message), and then re-execute the statements you want the transaction to contain.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> ROLLBACK TO SAVEPOINT cockroach_restart;
```

For examples of retrying transactions in an application, check out the transaction code samples in our <InternalLink path="example-apps">Build an App with CockroachDB</InternalLink> tutorials.

## See also

* <InternalLink path="savepoint">`SAVEPOINT`</InternalLink>
* <InternalLink path="transactions">Transactions</InternalLink>
* <InternalLink path="begin-transaction">`BEGIN`</InternalLink>
* <InternalLink path="commit-transaction">`COMMIT`</InternalLink>
* <InternalLink path="release-savepoint">`RELEASE SAVEPOINT`</InternalLink>
* <InternalLink path="show-savepoint-status">`SHOW SAVEPOINT STATUS`</InternalLink>
