# Rotate the secrets key

> Add a new version of the realm's secrets key, and move the realm's own secrets onto it as they are written.

Your key policy may ask for keys to be replaced on a schedule. Rotating the realm's secrets key, which seals the
realm's own vault, adds a new version: everything written from then on is sealed under it, and everything written
before still opens. No one notices.

**Before you start:** you are a realm administrator holding `RealmOwner` in this realm or a realm above it, signed in
with your session in `$CVYUH_SESSION` ([Call the API](/develop/api/) shows how). Users' and groups' vaults are under their own keys, which this doesn't
touch.

## Rotate it

<Steps>

1. Rotate the key:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/secrets/encryption \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{}'
   ```

   The realm answers `200` with the new `version`. Note it, with the time: a rotation isn't on the realm's audit
   trail yet.

2. Each secret in the realm's vault moves onto the new version the next time it is written, every version it holds
   sealed again. To move one now, write it again, here `infra/db-root`:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/realm/data/infra/db-root \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "data": { "user": "root", "password": "<password>" } }'
   ```

</Steps>

## Check it worked

Read a secret written before the rotation: it still opens. No answer shows which key version a secret is under yet, so
to be sure every secret has moved, write each one again: list them with a `GET` on
`https://foobar.cvyuh.com/api/v0/vault/managed/v1/realm/metadata?list=true`.

## Good to know

- **A secret not written since stays under the older version,** which still opens it. There is no call yet that seals
  every secret again at once: see
  [What's next for keys and protection](/secrets-store/keys-and-protection/whats-next/#realm-wide-reseal).

## Related

- [Key rotation](/secrets-store/keys-and-protection/key-rotation/): how it works, and when a user's key rotates.
- [Shred a realm's secrets](/secrets-store/guides/shred-a-realms-secrets/): destroy the key instead.
