Sub topic 11
Signing out and clearing cached credentials
This chapter covers what aws sso logout removes, what it leaves behind, and how to confirm that a machine holds no usable credentials. It assumes you can already sign in with aws sso login, you know what sts get-caller-identity returns, and you know the profile layout from the earlier chapters. You should also know the credential resolution order.
Why sign-out matters
Temporary credentials expire on their own, but "eventually" is not always soon enough. You may be leaving a shared workstation, handing a laptop to someone else, or finishing a session in a sensitive account. Signing out discards the local material that lets the CLI get new credentials without asking you to authenticate again.
The CLI stores two kinds of secrets on disk after a successful sign-in:
- The SSO access token, which proves you signed in to IAM Identity Center. The CLI uses it to request role credentials.
- The role credentials, which are the access key ID, secret access key, and session token for the role in a specific account. The CLI sends these on actual API calls.
Anyone who can read your home directory while these files are valid can use them. Signing out removes both kinds from the local cache.
What aws sso logout does
aws sso logoutThe command takes no profile argument. It signs out of all sessions associated with your CLI installation:
- It deletes the cached SSO access tokens, which are stored under
~/.aws/sso/cache. - It deletes the cached role credentials that were derived from those tokens, which are stored under
~/.aws/cli/cache.
It does not edit ~/.aws/config. Your sso-session block and your profiles stay exactly as they were, so the next aws sso login --profile dev works without any reconfiguration.
The next command that needs credentials from an Identity Center profile now fails until you sign in again:
aws sts get-caller-identity --profile devExpect an error saying the SSO token is missing, expired, or needs a new login. The exact wording differs between CLI versions, but the fix is always aws sso login.
What logout does not clear
Logout clears the CLI's own cache. It cannot reach credentials that were copied somewhere else. Three cases matter in practice.
Exported environment variables
The previous chapter's pattern, eval "$(aws configure export-credentials --profile dev --format env)", places credentials in your shell's environment. Those variables are not part of the CLI cache, so logout leaves them alone. Because environment variables rank above profiles in the resolution order, the CLI keeps using them even after you sign out. A command that seems to prove you are signed out may still succeed.
Clear them explicitly:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_CREDENTIAL_EXPIRATION
unset AWS_PROFILEUnsetting AWS_PROFILE is optional for security. It is good hygiene, because a leftover value can point later commands at an account you did not intend.
Credentials written to files or other tools
If you pasted credentials into a .env file, a container's environment, or another tool's settings, logout does not know about them. They remain valid until they expire. Find and delete those copies yourself.
Role sessions already issued
Logout is a local operation on your machine's cache. Role credentials that were already issued stay valid until their own expiration time, even if you delete every cached file. This is the main reason to keep the permission set's session duration short for sensitive accounts, as covered in the lifetimes chapter. Treat sign-out as a way to stop new credentials from being fetched. It does not cancel old ones.
| Item | Cleared by `aws sso logout` | How to clear it |
|---|---|---|
| Cached SSO access token in `~/.aws/sso/cache` | Yes | Not needed |
| Cached role credentials in `~/.aws/cli/cache` | Yes | Not needed |
| `sso-session` and profile blocks in `~/.aws/config` | No, by design | Edit the file if you want them gone |
| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` in a shell | No | `unset` them, or close the shell |
| Credentials copied to files or other tools | No | Delete them manually |
| Role credentials already issued | No | They expire on schedule |
Verifying a clean state
After logout, check three things: the cache directories, the environment, and the CLI's own view.
# 1. Cache directories should hold no token or credential files
ls ~/.aws/sso/cache ~/.aws/cli/cache
# 2. No credential variables should be set in this shell
env | grep '^AWS_' | grep -v -E 'SECRET|SESSION_TOKEN'
# 3. The CLI should refuse to produce an identity
aws sts get-caller-identity --profile devThe grep -v in the second command keeps secret values off your screen. It lists only the names and harmless values of any AWS_ variables still set. If AWS_ACCESS_KEY_ID appears, the CLI is still able to authenticate through the environment.
The third command should fail. If it succeeds, find out why before you assume you are signed out. Run it without --profile as well, because a default profile or environment variables may be supplying credentials. The identity it prints tells you which source was used.
Clearing the cache by hand
Sometimes aws sso logout is not enough. A cache file may be corrupted, a profile may be stuck on a stale session after you edited sso_start_url, or you want to rule out caching as the cause of a bug. You can delete the files directly:
rm -f ~/.aws/sso/cache/*.json
rm -f ~/.aws/cli/cache/*.jsonThis is safe. The cache holds only values the CLI can fetch again, so the worst outcome is another browser sign-in. Do not delete ~/.aws/config or ~/.aws/credentials this way. Those files hold your configuration and are not cache.
The file names in ~/.aws/sso/cache are hashes of the session name or start URL, so you cannot tell which file belongs to which session by reading the names. If you only want to drop one session while keeping another, inspect the JSON for the startUrl field and delete just that file. In most cases, running the logout command and signing in again costs less effort.
Signing out of the browser session too
aws sso logout ends the CLI's use of your Identity Center session. It does not necessarily end the web session in your browser. The AWS access portal keeps its own session, which is why a later aws sso login may complete without asking for your password. This is expected.
On a shared or untrusted computer, finish the job:
- Run
aws sso logout. - Unset any exported credential variables.
- Sign out of the AWS access portal in the browser, or close the browser profile you used.
On a personal laptop with disk encryption and a screen lock, the first two steps are usually enough.
When to sign out
Sign-out does not need to be a daily ritual. The short lifetimes already limit exposure. These situations call for it:
- You are done on a shared, borrowed, or pooled machine.
- You changed
sso_start_url,sso_region, or the registration scopes in ansso-sessionblock, and old tokens may no longer match. - You are troubleshooting and want to rule out stale cache.
- You exported credentials and want a clean slate before running a script with a different identity.
- Your laptop is about to be repaired, returned, or reimaged.
Common mistakes
- Assuming logout cleared the shell. The most frequent surprise is a command that still works after logout because
AWS_ACCESS_KEY_IDis set. Check the environment first. - Expecting logout to take a profile name. It affects all sessions. If you work across several sso-session blocks, you sign in to each again afterward.
- Deleting
~/.aws/configto "reset". This removes your profiles and sso-session blocks, and you would have to runaws configure ssoagain. Delete only the cache. - Skipping the verification. A successful
aws sso logoutmessage is not proof that no other credential source exists. Runget-caller-identityand read the result.
Exercise
You run eval "$(aws configure export-credentials --profile dev --format env)", then aws sso logout. You then run aws sts get-caller-identity with no --profile flag, and it still prints the dev role's ARN. Why does this happen, and what do you run to get the command to fail as expected?
Show answer
The export placed AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN (and AWS_CREDENTIAL_EXPIRATION) in the shell's environment. aws sso logout only deletes the CLI's cache files under ~/.aws/sso/cache and ~/.aws/cli/cache. Environment variables rank above profiles in credential resolution, so the CLI keeps using them until they expire. Run unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_CREDENTIAL_EXPIRATION (and optionally unset AWS_PROFILE), then repeat the identity check.
Exercise
Name two things aws sso logout deletes and two things it leaves in place.
Show answer
It deletes the cached SSO access tokens (~/.aws/sso/cache) and the cached role credentials (~/.aws/cli/cache). It leaves the sso-session and profile blocks in ~/.aws/config, and any credentials already exported to environment variables or copied into files. Already-issued role credentials also stay valid until they expire.