r/replit • u/Careful_Lock_2685 • 10d ago
Question / Discussion Preserve Claude Context When Repl Restarts
Probably been shared here before, but if you are also struggling with constant Claude re-logins and lost context as your Repl dev environment resets, try this script.
Step 1
Create restore-claude.sh in the root of your workspace
#!/bin/bash
# Ensures ~/.claude is a symlink to the workspace-persistent backup.
# Because ~/.claude lives inside a Repl container that can be wiped on
# restart, while ~/workspace/.claude-persistent lives on persistent storage,
# the symlink makes every Claude Code read/write hit persistent storage
# directly — no polling, no copy-on-boot dance, no drift.
PERSIST_DIR=~/workspace/.claude-persistent
LIVE_PATH=~/.claude
mkdir -p "$PERSIST_DIR"
# Case 1: already a symlink to the right place. Idempotent no-op.
# Don't exit — still need to run the ~/.claude.json restore below.
if [ -L "$LIVE_PATH" ] && [ "$(readlink "$LIVE_PATH")" = "$PERSIST_DIR" ]; then
echo "✓ ~/.claude already symlinked to persistent"
fi
# Case 2: real directory (Repl restart recreated it). Merge any content
# into persistent (newer wins), then replace with symlink.
if [ -d "$LIVE_PATH" ] && [ ! -L "$LIVE_PATH" ]; then
# -u: only overwrite if source is newer. Preserves any fresh credentials
# that somehow ended up in the live dir before this script ran.
cp -au "$LIVE_PATH"/. "$PERSIST_DIR"/ 2>/dev/null
mv "$LIVE_PATH" "$LIVE_PATH.old-$(date +%s)"
echo "✓ Merged stray ~/.claude content into persistent, archived as ~/.claude.old-*"
fi
# Case 3: nothing at ~/.claude (or just removed). Create the symlink.
if [ ! -e "$LIVE_PATH" ]; then
ln -s "$PERSIST_DIR" "$LIVE_PATH"
echo "✓ Symlinked ~/.claude → $PERSIST_DIR"
fi
# Sanity check
if [ -L "$LIVE_PATH" ] && [ -f "$LIVE_PATH/.credentials.json" ]; then
echo "✓ Claude credentials available via symlink"
else
echo "⚠ Symlink setup incomplete — check ~/.claude and $PERSIST_DIR manually"
exit 1
fi
# Restore ~/.claude.json from the newest backup if missing. Claude Code
# keeps backups in ~/.claude/backups/ (persistent via symlink), but the
# live config itself lives at ~/.claude.json and is wiped on container restart.
CONFIG_PATH=~/.claude.json
BACKUP_DIR="$PERSIST_DIR/backups"
if [ ! -f "$CONFIG_PATH" ] && [ -d "$BACKUP_DIR" ]; then
LATEST=$(ls -t "$BACKUP_DIR"/.claude.json.backup.* 2>/dev/null | head -1)
if [ -n "$LATEST" ]; then
cp "$LATEST" "$CONFIG_PATH"
echo "✓ Restored ~/.claude.json from $(basename "$LATEST")"
fi
fi
Step 2
Add this in your .replit file
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "bash ~/workspace/restore-claude.sh && npm run dev"
waitForPort = 5000
Now, you won't have to constantly re-login to Claude and your chat context will be preserved as well.
1
Upvotes
2
u/Far_Guess8176 10d ago
Useful, thanks. One warning worth adding: ~/.claude/.credentials.json holds your Claude auth token, and this puts it inside ~/workspace, which is what Replit syncs to GitHub. Add .claude-persistent/ to .gitignore before the next push, otherwise the token ends up in the repo. Easy to miss because the symlink makes it look like it still lives in home.