HOW-TO GUIDE

How to find and purge API keys Claude Code and Codex saved on your laptop

Based on a guide by The Weekly Stack on DEV Community · Nov 30, 2026

You'll end with: A laptop where any keys your coding agents saved in transcripts and history files are found, rotated, purged, and kept for 7 days at most.

Why it's worth having: Claude Code keeps plain-text session transcripts for 30 days by default, so a .env file a tool read or a key a command printed sits on your disk.

At a glance

WHO IT'S FOR
IT and OT engineers and developers who run Claude Code or Codex on their own machines.
LEVEL
Beginner
COST
Free. The cleanup uses grep and two settings files, with no new tools.
YOU NEED
Claude Code or Codex · A Mac or Linux terminal · Access to your API provider dashboards

The method, in short

  1. 1Search ~/.claude/projects, ~/.claude/history.jsonl, and ~/.codex/history.jsonl for anything shaped like a key or a secret.
  2. 2Revoke and reissue any real key in the provider's dashboard before deleting files, since a backup can already hold a copy.
  3. 3Preview claude purge with --dry-run, run it to remove a project's transcripts, and delete or trim the Codex history file.
  4. 4Set cleanupPeriodDays to 7, deny Read on .env files, and set Codex history persistence to none.

The post has the search pattern, the exact settings for both agents, and a no-history mode for scripted runs.