
Want Claude Code to ignore files like .env, node_modules or a secrets folder? The short answer: add Read deny rules to .claude/settings.json. A rule such as Read(./.env) stops Claude’s file tools from opening the file, keeps it out of @ mentions and blocks shell commands like cat .env.
A .claudeignore file does nothing. Claude Code never reads it, and the old ignorePatterns setting is deprecated in favor of permissions.deny. Your .gitignore helps a little, because searches and the @ file picker skip gitignored files, but Claude can still open a gitignored file when it asks for it by name.
We proved all three points in a real session below. With only .gitignore and .claudeignore in place, Claude read a fake API key straight out of .env. With five deny rules, the same request failed, and so did cat .env.
Browse all our Claude Code tutorials for more fixes and how-tos.
Tested with Claude Code v2.1.332 on macOS on November 29, 2026. We used a throwaway demo project called shop-app with a fake .env file (the values say NOT-REAL), a tiny node_modules folder and a secrets folder, all inside a git repository.
We tested on macOS; the Windows and Linux steps follow Anthropic’s official instructions. The rule syntax is the same everywhere, except that Windows paths are converted to forward-slash form before matching.
Quick Summary
| Method | What it hides from Claude | Stops a direct read? | Tested by us? |
|---|---|---|---|
.claudeignore file | Nothing | No | Yes |
.gitignore | Search results and the @ picker | No | Yes |
Read(...) deny rule | File tools, @ mentions, cat, head, sed, tee | Yes | Yes |
Edit(...) deny rule | Every tool that edits files | No, edits only | From the docs |
| Sandbox with strict mode | Shell commands and the scripts they start | Yes, at the OS level | From the docs |
Why Claude Code Doesn’t Ignore Files on Its Own
Claude Code works inside your project folder, and by default it can read any file there. That is what makes it useful: it opens your code, follows imports and checks config files without asking each time.
The catch is that “any file” includes your .env with API keys, a secrets folder and thousands of files in node_modules. Many people assume Claude follows .gitignore the way git does, or that a .claudeignore file works like .dockerignore. Neither is true for direct reads.
Anthropic’s permissions documentation says it plainly: if your project has a .claudeignore file, it has no effect, so move its entries into Read deny rules. For a wider look at what Claude Code sends and stores, see our guide to Claude Code privacy.
Our test project
We set up the classic trap. The .gitignore lists .env, node_modules/ and secrets/, and a .claudeignore file lists the same three entries. Only five files are tracked by git.

What .gitignore Does (and Doesn’t) Hide From Claude Code
Your .gitignore isn’t useless. It changes two things inside Claude Code, and both showed up in our session.
The @ file picker skips gitignored files
When you type @ to mention a file, the picker leaves out files that match .gitignore. That comes from the respectGitignore setting, which is on by default and appears in /config as “Respect .gitignore in file picker”. We typed @.en and got .env.example and .claudeignore, but not .env.

Searches skip gitignored files
Next we asked, “What is DEMO_API_KEY set to in this project?” Claude searched for the name and found it only in .env.example, where the value is empty. The real .env never came up, because the search skipped gitignored files.

The docs explain the details. The Grep tool respects .gitignore, while the Glob tool does not unless you set CLAUDE_CODE_GLOB_NO_IGNORE=false before launching. On macOS and Linux, Claude usually searches with find and grep in its shell instead of those tools, so results can vary.
But a direct read still works
Here is the part that surprises people. In a fresh session we asked Claude to “Read the .env file and tell me what DEMO_API_KEY is.” It opened the file at once and printed the fake key, even with .env in both .gitignore and .claudeignore.

Gitignore hides a file from searches. It does not lock the door.
How to Make Claude Code Ignore Files With Read Deny Rules
Deny rules are the official way to keep files away from Claude Code. They live in the permissions.deny list of a settings file, and deny always wins over allow. Our permission rules guide covers the full rule system; here is the part you need for ignoring files.
Step 1: Create the settings file
In your project root, create .claude/settings.json if it doesn’t exist. Use this file to share the rules with your team through git. For rules only you need, use .claude/settings.local.json instead.
Step 2: Add the deny rules
This is the exact file we used. It blocks .env, every .env.* variant except .env.example, the whole secrets folder and node_modules:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(!.env.example)",
"Read(./secrets/**)",
"Read(node_modules/**)"
]
}
}
Settings files are strict JSON. A // comment or a trailing comma makes Claude Code skip the whole file, which is the top cause in our settings.json fixes post.
Step 3: Check that the rules loaded
Start Claude Code in the project root and run /permissions. Press the right arrow until the Deny tab is selected. All five rules showed up for us, loaded from the project.

Step 4: Test it
We asked Claude to read both .env.example and .env and list the keys. It read the example file fine, thanks to the ! carve-out, and reported that it couldn’t read .env because the permission settings deny access.

Then we asked it to run cat .env in the shell. Claude Code refused before the command ran, with the reason “Permission to use Bash with command cat .env has been denied” and a note that the rule came from shared project settings.

Patterns for .env, node_modules and Other Folders
Read and Edit rules use gitignore pattern syntax. The leading characters decide where a pattern starts, which trips up a lot of people.
| Rule | What it blocks |
|---|---|
Read(./.env) or Read(.env) | Any .env at or under the current directory |
Read(./.env.*) | .env.local, .env.production and similar |
Read(!.env.example) | Carves .env.example back out; list it after the rule it narrows |
Read(./secrets/**) | A secrets folder at any depth, as a deny rule |
Read(node_modules/**) | Every node_modules folder, nested ones included |
Read(//**/.env) | Any .env anywhere on the disk |
Read(~/.aws/**) | A folder in your home directory |
Watch out for the single slash. /path means “relative to the settings file’s location”, not the root of your disk.
In ~/.claude/settings.json, Read(/secrets/**) blocks ~/.claude/secrets, not your project’s folder. To block a file in every project from your user settings, use // or ~/ paths.
Two more limits from the docs: a ! carve-out only affects rules in the same settings file, and it can’t reopen a file inside a folder that a rule blocks as a whole. With Read(secrets/**) and Read(!secrets/public/**), all of secrets stays blocked.
Should you block node_modules?
It depends on why you want it ignored. If the goal is less noise, .gitignore already keeps node_modules out of searches and the @ picker. A deny rule goes further and stops Claude from reading any dependency’s source, which can hurt when it needs to check how a library works.

What Deny Rules Don’t Cover
Deny rules are strong, but they are not a vault. Anthropic’s docs list the gaps clearly, and you should know them before you trust a rule with a real production key.
- Commands that read files without naming them, such as
grep -r pattern .run from the folder that holds the file. - Scripts that open files themselves, such as a Python or Node program.
- The NotebookEdit tool, which a
Readrule doesn’t cover. Add anEditdeny rule for paths no tool may change. - A
Bashdeny rule matches the command as written, soBash(curl *)doesn’t stop/usr/bin/curl.
We tested the script gap with python3 -c "print(open('.env').read())". Claude declined on its own, saying the command would get around the block. That was the model’s judgment, not a technical lock, so don’t count on it.

Close the gap with the sandbox
For OS-level enforcement, turn on the Claude Code sandbox. The docs say paths from Read and Edit deny rules merge into the sandbox configuration, so sandboxed commands and their child processes can’t read those files either. Turning off the unsandboxed retry keeps commands inside it:
{
"sandbox": {
"enabled": true,
"allowUnsandboxedCommands": false
},
"permissions": {
"deny": ["Read(./.env)", "Read(./secrets/**)"]
}
}We didn’t run the sandbox in this test; this step follows Anthropic’s docs. Our sandbox walkthrough shows it in action. Commands listed in excludedCommands still run outside the sandbox with your full access.

Real Examples You Can Copy
A Node.js project
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(!.env.example)",
"Read(./dist/**)",
"Read(./coverage/**)"
]
}
}A Python project
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.venv/**)",
"Read(./config/credentials.json)",
"Read(**/*.pem)"
]
}
}Every project on your machine
Put this in ~/.claude/settings.json to block common secret files everywhere. The // prefix anchors at the root of your disk, so the rules work no matter which project you open.
{
"permissions": {
"deny": [
"Read(//**/.env)",
"Read(~/.ssh/**)",
"Read(~/.aws/**)"
]
}
}If you also pass API keys to MCP servers, our guide to MCP env vars shows how to keep them out of shared files.
Try It Yourself
Make a throwaway folder with a fake .env (use a value like demo-NOT-REAL), add the deny rules from Step 2, start Claude Code there and paste this prompt:
Read .env.example and .env, then list the keys in each file. One line each.
After that, run this shell command and show its output: cat .envYou should see the example file listed and both attempts on .env blocked. Remove the rule, start a new session and run the same prompt to see the difference.
Common Mistakes
- Writing a
.claudeignorefile. It has no effect at all. - Using the old
ignorePatternskey. It’s deprecated; usepermissions.deny. - Writing
Grep(secrets/**)orGlob(secrets/**). These rules are accepted but never match; writeRead(secrets/**)instead. - Using
/pathin user settings and expecting it to mean your project. - Listing a
!carve-out before the rule it should narrow. A carve-out listed first carves nothing. - Breaking the JSON with a comment, so the whole file, rules included, is skipped. The common errors guide lists the warning you’ll see.
Pro Tips
- Keep real values out of
.env.exampleand allow it with a carve-out, so Claude still knows which keys your app needs. - Run
/permissionsafter every change; the Deny tab shows exactly what loaded. - A
Readdeny rule also blocks Edit and Write on the same path, so Claude can’t overwrite.enveither. - Rotate any key Claude already printed in a session. Blocking it now doesn’t remove it from earlier transcripts.
- Combine deny rules with the sandbox for anything that would hurt if it leaked.
Cheat Sheet: Make Claude Code Ignore Files

Frequently Asked Questions
Does Claude Code support a .claudeignore file?
No. Anthropic’s docs say a .claudeignore file has no effect. Move its entries into Read deny rules in .claude/settings.json.
Does Claude Code respect .gitignore?
Partly. Searches and the @ file picker skip gitignored files, but Claude can still read a gitignored file by name. In our test it read a gitignored .env without any prompt.
How do I stop Claude Code from reading my .env file?
Add "Read(./.env)" and "Read(./.env.*)" to permissions.deny in .claude/settings.json, then confirm them in the Deny tab of /permissions.
Can Claude Code still see a denied file through Bash?
Not with commands it recognizes, like cat, head, tail, sed and tee. Scripts and commands that don’t name the file aren’t covered, so enable the sandbox for OS-level protection.
Do deny rules make Claude Code faster in big projects?
Not directly. They control access, not speed. To cut search noise from folders like node_modules, make sure they’re in .gitignore.
Conclusion
To make Claude Code ignore files, skip .claudeignore and go straight to Read deny rules: in our test, .gitignore kept .env out of searches and the @ picker, yet Claude still read the fake key by name until five deny rules in .claude/settings.json blocked both the read and cat .env. Use .gitignore for noise like node_modules, deny rules for secrets, a ! carve-out for .env.example, and the sandbox when scripts must not reach a file either. To learn more about the model behind it, see our Claude AI overview.


