How to Delete API Keys Your AI Coding Agent Saved on Your Laptop (Claude Code and Codex)

2026-11-30来源 dev.toEnglish

Claude Code saves every session as a plain-text transcript in your home folder for 30 days, and Codex saves a history file by default. If your agent ever read a .env file or printed a key, that key is sitting on your disk.

This cleanup takes about 10 minutes and needs no new tools: search those folders, rotate anything you find, purge, then shorten how long they keep history.

Why it matters

Anthropic's own docs are blunt: Claude Code transcripts "are not encrypted at rest" and "if a tool reads a .env file or a command prints a credential, that value is written" to the session file. Laptops get backed up, synced and lost. Running several agents? Check every agent's data folder, not just one.

1. Know where it lives

  • Claude Code: transcripts in ~/.claude/projects/ (deleted after 30 days by default) and your typed prompts in ~/.claude/history.jsonl (kept until you delete them).
  • Codex: ~/.codex/history.jsonl.

2. Search for keys

Run this on Mac or Linux. Every file it lists contains something that looks like a secret.

grep -rlE "sk-[A-Za-z0-9_-]{16,}|_KEY=|SECRET|PRIVATE KEY" \ ~/.claude/projects ~/.claude/history.jsonl ~/.codex/history.jsonl 2>/dev/null 

3. Rotate first, delete second

If a real key shows up, revoke it and make a new one in that provider's dashboard. Deleting the file doesn't help if a backup already copied it.

4. Purge the project

Update Claude Code, then preview what would be removed:

claude purge ~/path/to/project --dry-run 

Run it again without --dry-run to delete that project's transcripts and matching history lines. It asks before removing anything.

For Codex, delete or trim ~/.codex/history.jsonl.

5. Keep less from now on

Add the settings below, and block your agent from reading .env files at all. Merge these into your existing files rather than replacing them.

Claude Code, in ~/.claude/settings.json:

{ "cleanupPeriodDays": 7, "permissions": { "deny": ["Read(./.env)", "Read(./.env.*)"] } } 

Codex, in ~/.codex/config.toml:

[history] persistence = "none" 

For throwaway scripted runs, set CLAUDE_CODE_SKIP_PROMPT_HISTORY=1 and nothing gets written.

Further reading: Claude Code docs · Codex docs

This was first published in The Weekly Stack, a free newsletter of AI tool tips for builders. Subscribe: https://weeklystackhq.beehiiv.com/subscribe

原文:https://www.a8q9.cn/en/article/how-to-delete-api-keys-your-ai-coding-agent-saved-on-your-laptop-claude-code-and-651d4e.html

相关文章