# Reverting Transactions

Source: https://docs.formance.com/v3.2/modules/ledger/working-with/reverting-transactions

Stack: v3.2

A revert creates a compensatory transaction with the exact opposite postings, cancelling the effect of the original. The original transaction remains in the immutable log; reverts are corrections, not deletions.

## When to use it

Use a revert when a transaction was committed by mistake, for example a duplicate payment, a wrong amount, or a processing error. Do not use reverts routinely as a workflow mechanism (e.g. to model a "pending → cancelled" state); model that with your account structure instead.

## How to revert

To revert a transaction, use the revert endpoint:

curl

```bash
curl -X POST $FORMANCE_API_URL/api/ledger/v2/my-ledger/transactions/TX_ID/revert
```

The ledger creates a compensatory transaction with opposite postings to cancel out the original transaction's effects.

## The atEffectiveDate parameter

The `atEffectiveDate` parameter controls the transaction time of the compensatory transaction:

Without `atEffectiveDate=true`, the compensatory transaction is created at the current time, which produces incorrect balances in historical reports and financial statements.

For example, consider an account with transactions 1, 2, and 3. If you revert transaction 2 without `atEffectiveDate`, the compensatory transaction is created at the current time (after transaction 3). When generating reports that filter out reverted transactions, transaction 3 will show an incorrect balance of -9250 instead of -9750 because the revert isn't accounted for at the right point in time.

With `atEffectiveDate=true`, the compensatory transaction is created with the same transaction time as the original, maintaining accurate historical balances:

| TxId | Amount In | Amount Out | Balance |
| ---- | --------- | ---------- | ------- |
| 1    | 0         | 10000      | -10000  |
| 2    | 500       | 0          | -9500   |
| 4    | 0         | 500        | -10000  |
| 3    | 250       | 0          | -9750   |

Always use `atEffectiveDate=true` when reverting transactions to maintain accurate historical balances.

## Force reverting

By default, the system prevents reverting transactions that would result in insufficient funds. However, you can override this validation using the `force` parameter.

To revert a transaction involving an account with a negative balance:

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/ledger/v2/my-ledger/transactions/TX_ID/revert?force=true"
```

Use the `force` parameter with caution:

- It allows creating negative balances in accounts
- Ensure proper controls and auditing are in place when using forced reversals
- This feature is particularly useful for accounts that act as "always negative" balance accounts (like liability accounts)

## Querying without reverts

When querying transactions, you may want to exclude both reverted transactions and their compensatory transactions. Use this filter:

```json
{
  "$and": [
    { "$match": { "account": "deals:XYZ:balances:" } },
    { "$match": { "reverted": false } },
    { "$not": { "$exists": { "metadata": "com.formance.spec/state/reverts" } } }
  ]
}
```

This query:

- Matches transactions for a specific account
- Excludes transactions marked as reverted
- Excludes compensatory transactions (identified by the `com.formance.spec/state/reverts` metadata)

For performance, consider using the `/volumes` endpoint instead of `/transactions` when you only need balance information. The volumes endpoint supports `startTime`, `endTime`, `insertionDate`, and `groupBy` query parameters; pass `insertionDate=true` for bi-temporal point-in-time queries.
