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

# clearPersistedSession

> Deletes the persisted session from the Keychain and forgets the last exchanged grant, so a later restoreSession returns nil.

**Function Signature**

```swift theme={null}
public func clearPersistedSession() async throws
```

A `PortalAuth` method. Removes the session persisted for this `authEnvironmentId`, and forgets the grant `handleRedirect` last exchanged and any two-factor session still waiting to be written, so neither a redirect re-delivered after sign-out nor a repeated `verifyTotp` can bring back the session that was just cleared.

**Throws**

* `PortalAuthError.sessionStorageFailure(message:)`: The Keychain delete failed.

**Notes**

* Call it on sign-out, alongside `portal.clearSession()`, and from your `onSessionInvalidated` listener. `Portal.clearSession()` invalidates the credential the `Portal` holds but does not reach `PortalAuth`'s memory of the last grant it exchanged.
* It is not a sign-out on its own. It does not affect a `Portal` that already holds the session (there is no server-side revoke); it prevents a future `restoreSession()` from returning it. Use `portal.clearSession()` for the live instance.
* Waits for an in-flight `handleRedirect` or `verifyTotp` to finish persisting before deleting, so a sign-in cannot write a session back after the delete.
* Keychain items survive app deletion, so calling this on the first launch after install gives a fresh install a clean slate.
* Available starting from SDK version 8.0.0.

**Example Usage**

```swift theme={null}
import PortalSwift

func signOut() async {
    // Run both, even if one fails: each clears something the other does not.
    do {
        try await portal?.clearSession()
    } catch {
        // The in-memory token is gone either way; a stale copy may remain in the Keychain.
        print("Error clearing the session: \(error)")
    }

    do {
        try await auth.clearPersistedSession()
    } catch {
        print("Error clearing the persisted session: \(error)")
    }
    portal = nil
    showSignIn()
}
```

**Related Documentation**

* [clearSession reference](./clearsession)
* [restoreSession reference](./portalauthrestoresession)
* [End the session](../guide/client-auth#end-the-session)
