DEV Community

Cover image for Dead code is a git problem before it is a linter problem
Raghav Chamadiya for repowise

Posted on AI-assisted

Dead code is a git problem before it is a linter problem

TL;DR

  • Linters find unused code inside a file: an unused import, a variable, a private function. A whole file that nothing imports, or code nobody has changed in years, only shows up in the import graph and in git history.
  • 170 of 190 popular repos (89%) have at least one production file nothing imports. In the median repo that is 3.2% of production files. Pooled: 19,697 files flagged as unreachable and 39,863 unused exports.
  • On Django, plain git log dated 90 of 92 flagged files to 2022 or later, because of repo-wide formatting commits. Skipping the commits in .git-blame-ignore-revs, 68 were last changed in 2020 or earlier.
  • Old plus unimported is a lead, not a verdict. The last step is always a string search.

Data: repowise's index of 195 public repos with 1,000+ GitHub stars, plus git and ripgrep on a full clone of Django.

Most teams treat unused code as a linter setting. Turn on no-unused-vars, Ruff's F401, noUnusedLocals, and the problem looks handled. They are useful, and they work one file at a time. The unused code that costs you the most is a whole module nobody imports and nobody has opened since a migration three years ago. No single-file check can see that. You need two things a linter does not read: who imports the file, and when it last changed.

How much unused code do popular repos carry?

From the 190 repos with dead-code data:

  • 182 of 190 (96%) have at least one dead-code finding in production code.
  • 170 of 190 (89%) have at least one production file nothing imports. In the median repo that is 3.2% of production files, and in 44 repos (23%) it is 10% or more.
  • Pooled, 19,697 production files are flagged as unreachable and 39,863 exports are unused, covering 3.5 million lines.

Single-file lint rules report neither kind. An unused export looks fine from inside its own file, and an unreachable file has nothing wrong in it at all.

What does git history say about a file nothing imports?

I took the 92 files repowise flags as unreachable in Django and asked git when each last changed. Plain git log first:

$ git log -1 --format=%ad --date=short -- django/conf/locale/bs/formats.py
2022-02-03
Enter fullscreen mode Exit fullscreen mode

That looks recent-ish. It is not:

$ git log -3 --format='%h %ad %s' --date=short -- django/conf/locale/bs/formats.py
9c19aff7c7 2022-02-03 Refs #33476 -- Reformatted code with Black.
82f286cf6f 2018-09-25 Refs #29784 -- Switched to https:// links where available.
d7b9aaa366 2016-11-19 Refs #23919 -- Removed encoding preambles and future imports
Enter fullscreen mode Exit fullscreen mode

The last commit is the repo-wide Black reformat. The admin JavaScript has the same story with a 2026 "Formatted JavaScript files." commit. Across all 92 files, plain git log put 90 of them in 2022 or later. Django lists its formatting commits in .git-blame-ignore-revs. Skip those, and 68 of the 92 were last really changed in 2020 or earlier.

So git age is a strong signal, but only after you remove the commits that touched everything.

Then the import check, which is where it gets humbling:

$ rg -n "locale\.bs" --type py

$ rg -n "conf\.locale" --type py django
django\utils\translation\__init__.py:270:    from django.conf.locale import LANG_INFO
django\utils\formats.py:76:    format_locations.append("django.conf.locale.%s")
django\utils\translation\trans_real.py:14:from django.conf.locale import LANG_INFO

$ rg -n "cancel\.js"
django\contrib\admin\templates\admin\delete_selected_confirmation.html:7:    <script src="{% static 'admin/js/cancel.js' %}" async {% csp_nonce_attr %}></script>
django\contrib\admin\templates\admin\delete_confirmation.html:7:    <script src="{% static 'admin/js/cancel.js' %}" async {% csp_nonce_attr %}></script>
Enter fullscreen mode Exit fullscreen mode

Nothing imports the Bosnian formats file by name, but django/utils/formats.py builds the module path as a string and imports it at runtime. cancel.js is served to the browser from two admin templates. The Sphinx files under docs/ are loaded by Sphinx. The Django files I looked at are all reached another way, the same thing I found when I hand-checked Django, LangChain and Airflow. In a well-kept repo, that is the expected result.

A routine for finding old, unused files

Step 1, the last real change for every file, skipping formatting commits:

git log --format='@%H %ad' --date=short --name-only \
  | awk 'NR==FNR{ig[$1]=1;next} /^@/{h=substr($1,2);d=$2;next} NF && !(h in ig) && !seen[$0]++ {print d, $0}' \
      <(grep -o '^[0-9a-f]\{40\}' .git-blame-ignore-revs) - \
  | sort > last-change.txt
Enter fullscreen mode Exit fullscreen mode

If your repo has no .git-blame-ignore-revs, start one. List the formatter commits there once and git blame uses it too (git config blame.ignoreRevsFile .git-blame-ignore-revs).

Step 2, keep files that still exist and have not changed in three years:

$ awk 'NR==FNR{t[$0]=1;next} ($2 in t) && $2 ~ /^django\/.*\.py$/ && $1 < "2023-11-17"' \
    <(git ls-files) last-change.txt > old.txt
$ wc -l < old.txt
449
$ head -3 old.txt
2005-07-13 django/core/__init__.py
2005-07-13 django/utils/__init__.py
2005-07-13 django/views/decorators/__init__.py
Enter fullscreen mode Exit fullscreen mode

449 of Django's 909 Python files under django/ have not really changed in three years. Old on its own means stable, not dead. django/core/__init__.py has been fine since 2005.

Step 3, keep the old files that nothing imports. Get the unreachable list from an import-graph tool (Knip for TypeScript, as in the Knip walkthrough, or repowise dead-code for any language), one path per line, and join:

$ awk 'NR==FNR{f[$0]=1;next} ($2 in f)' unreachable.txt old.txt | wc -l
75
Enter fullscreen mode Exit fullscreen mode

From 449 old files to 75 old and unimported. All 75 are Django locale formats.py files, and step 4 is what clears them.

Step 4, search for each candidate as a string: its dotted module name, its file stem, its path in templates, configs, package.json, pyproject.toml entry points and CI files. If a prefix shows up next to %s, import_module, require( or a glob, the file is loaded by name.

repowise joins the import graph and git age in one pass. A file nothing reaches gets its confidence from git: any commit in the last 90 days caps it low, and a year without one gives the top score. How that scoring works is written up separately. Step 4 still belongs to you.

When is deleting unused code safe?

When all of these hold:

  1. Nothing imports it, and a string search for its name and path finds nothing.
  2. It has not really changed in a long time, after skipping formatting commits.
  3. It is not part of a published package's public surface. Users import those paths from outside your repo.
  4. The tests pass and every module still imports after the delete.

Delete it in a small pull request. Git keeps it. If someone needs it next year, git log --diff-filter=D -- path/to/file finds the deleting commit and git restore --source=<sha>^ -- path/to/file brings it back. That is the other half of why this is a git problem. Deleting is cheap because history is the backup.

How I got these numbers

The cohort numbers come from 195 of the most-starred public repositories indexed on repowise.dev (1,000+ GitHub stars each, 253,688 production source files, November 2026), with dead-code data for 190 of them. Findings are counted on production files at confidence 0.4 and above, each repo at its own latest index. The full breakdown is in the code health 2026 report. The Django run used a full clone at d3954c9 (12 November 2026), git 2.51 and ripgrep 15.1. The 92 flagged files come from a repowise 0.60.1 index of the same clone at 6a8248f, and none of them changed between the two commits.

FAQ

What is unused code?
Code that nothing in the program reaches: a variable or import never read, a function never called, an exported symbol no other module imports, or a whole file no other file imports and no entry point loads. Linters cover the first two kinds inside one file. The last two need the import graph of the whole repo.

How do I find unused code in a large codebase?
Run your linter's unused rules for names inside files, then an import-graph tool for unused exports and files (Knip for JavaScript and TypeScript, vulture for Python names, repowise for files and exports across languages). Sort the results by last real change in git and search each candidate's name as a string before deleting.

Is it safe to remove unused code?
It is safe when nothing imports it, no string or config refers to it, it is not part of a published API, and the tests and an import of every module pass afterwards. Git keeps every deleted file, so a wrong call is one git restore away.

Top comments (0)