Can I delete .pytest_cache? Yes, and here's what it does

Yes, it's safe to deleteDiskmack safety tier: Safe to clean
**/.pytest_cache

Yes, .pytest_cache is safe to delete. It's pytest's test-run cache: a small folder that mostly remembers which tests failed last time, and pytest recreates it automatically on the next run. Deleting it never touches your code or your test files. The only thing you lose is one run's worth of history for flags like --lf (rerun last failures).

What it is

The .pytest_cache folder is created by pytest's cache plugin, which ships with pytest and is on by default. Every time you run your tests, pytest writes a little state into it: the list of tests that failed (so --lf and --ff can rerun or prioritize them), the node ID it stopped at when you use --sw (stepwise mode), and any values that other plugins choose to stash between runs. It appears in the root of whichever project you ran pytest in, so if you work on several Python projects, you'll have several copies.

It grows very slowly, and honestly, not much. A typical .pytest_cache is a few kilobytes to a few megabytes, even on large test suites. If your Mac is out of space, this folder is almost never the culprit. The real Python disk hogs are virtual environments, the pip cache, and __pycache__ folders scattered through your projects. Still, .pytest_cache is pure regenerable state, so there's no reason to keep it if you're tidying up or resetting a confused test run.

Is it safe to delete?

This is about as safe as deleting gets. The folder holds no source code, no test results you'd want to archive, and no configuration. If you delete it, the next pytest run simply starts with a clean slate: --lf and --ff behave as if every test passed last time (so they run the full suite once), stepwise mode forgets where it stopped, and plugin-cached values get recomputed. That's the entire cost.

Deleting it is also a legitimate fix, not just cleanup. If pytest's last-failed selection seems stuck on tests you've already fixed, or a plugin is serving stale cached data, removing .pytest_cache resets that state cleanly. Diskmack finds every .pytest_cache folder across your projects automatically and clears them the safe way.

How to check its size

In Finder: In Finder, choose Go > Go to Folder (Cmd-Shift-G) and enter the path to your project, for example ~/Projects/my-app. Hidden files don't show by default, so press Cmd-Shift-. (period) to reveal them, then select .pytest_cache and press Cmd-I to see its size.

In Terminal:

du -sh ~/Projects/my-app/.pytest_cache

Swap in your actual project path. To find and measure every copy under your code folder: find ~/Projects -type d -name .pytest_cache -prune -exec du -sh {} +, adjusting ~/Projects to wherever your repos live.

How to clean it

  1. Open your project folder in Finder.
  2. Press Cmd-Shift-. (period) to show hidden files if you don't see the folder.
  3. Drag .pytest_cache to the Trash.
  4. Repeat for any other projects where you run pytest, then empty the Trash.

pytest has a built-in equivalent: run pytest --cache-clear and it wipes the cache contents at the start of that test run.

Will it come back?

Yes, immediately. The next time you run pytest in that project, the folder reappears with fresh state from that run. That's expected and fine. Because it regrows so fast and stays so small, deleting it won't win you meaningful disk space; treat it as a reset button for pytest's memory rather than a cleanup target. If it bothers you in git status, add .pytest_cache/ to your .gitignore, which recent pytest versions actually set up for you inside the folder itself.

Common questions

Will deleting .pytest_cache break my tests?

No. Your tests live in your own files; the cache only stores metadata about previous runs. Worst case, the first pytest --lf after deletion runs the full suite because there's no failure history to filter by.

Why does .pytest_cache keep showing up in my repo?

The cache plugin is enabled by default, so any pytest run creates it. Add .pytest_cache/ to your project's .gitignore or your global gitignore. Modern pytest writes a .gitignore inside the folder itself, so most git setups already ignore it.

Can I stop pytest from creating it at all?

Yes. Run pytest with -p no:cacheprovider, or add that to addopts in your pytest.ini or pyproject.toml. The trade-off is losing --lf, --ff, and stepwise mode, which are genuinely useful, so most people just gitignore the folder instead.

How much space does it actually use?

Very little. Expect kilobytes per project, maybe a few megabytes on huge suites. If you're hunting for real space, look at virtual environments, the pip cache, and __pycache__ folders instead.

Related folders