There is a function in your codebase that nobody has called in two years. It was written for a feature that was cancelled, or a customer who left, or an idea that never shipped. Everyone knows it is dead. Nobody deletes it. Deleting it feels risky, and keeping it feels free.
Keeping it is not free. Dead code costs your team every day, in small ways that add up. It shows up in searches. It confuses new developers. It gets copied into new features because someone assumed it was still used. It breaks builds when a dependency changes. And when someone finally does try to remove it, the job is harder, because by then nobody remembers what it was for.
Deleting code is a skill, and it is one of the most underrated skills on a development team. Writing code gets the praise. Deleting code gets the suspicion. That is backwards. A codebase that grows without ever shrinking becomes harder to understand, harder to change, and harder to trust.
Why nobody deletes anything
The reasons are understandable. The first is fear. What if something still calls this? What if a customer still uses that endpoint? What if deleting it breaks a report that runs once a quarter? The fear is reasonable, and the way to handle it is not to be brave. The way to handle it is to be sure.
The second reason is ownership. The person who wrote the code has left, or moved to a different team, and nobody feels authorized to remove their work. Deleting someone else’s code feels like a judgment on it. It is not. Code is not a monument. It is a working tool, and tools that are no longer used should be put away.
The third reason is that deletion has no visible reward. Nobody gets praised for removing three hundred lines. The work is invisible. The codebase is slightly smaller, slightly clearer, and nobody notices. The person who wrote a new feature gets the credit. The person who deleted the old one gets nothing. Teams that do not celebrate deletion will slowly stop doing it.
There is also a quieter reason. Some developers keep dead code as a safety blanket. If the feature comes back, the code is still there. That is a reasonable instinct, and it is wrong, because version control already does that job. The code is not gone. It is in the history, waiting to be restored if it is ever needed. Deleting it from the current codebase does not destroy it. It moves it to the archive, where it belongs.
How to delete safely
Safe deletion is a process, not a leap of faith. Start by proving the code is dead. Search for every reference to it. Check the logs to see whether the endpoint is ever called. Check the analytics to see whether the feature is ever used. Check the database for records the code creates. If you cannot find a single caller, a single log entry, or a single user, the code is dead.
Then delete it in a small, reviewable change. One function, one module, one endpoint at a time. Do not combine a deletion with a new feature in the same change. A deletion on its own is easy to review and easy to revert. A deletion buried inside a large change is hard to review and hard to undo.
Run the full test suite after the deletion. If the tests pass, that is evidence the code was dead. If a test fails, read it. A failing test might be the only evidence that something still depends on the code you removed. That is useful information, and it is better to learn it from a test than from a customer.
For anything you are unsure about, use a deprecation step first. Mark the endpoint as deprecated, log every call to it for a month, and delete it when the log is empty. That turns a guess into data. It takes longer, but it removes the fear, and the fear is what stops most deletions.
What deletion buys you
A smaller codebase is easier to understand. A new developer who opens the project sees the code that matters, not a museum of past ideas. They learn the system faster, and they make fewer mistakes, because there is less irrelevant code to confuse them.
A smaller codebase is easier to change. Every line of code is a line that has to be considered when you make a change. Dead code adds to that burden without adding value. It shows up in searches, it gets in the way of refactoring, and it makes every change slightly harder than it needs to be.
A smaller codebase is also easier to secure. Dead code can contain vulnerabilities that nobody is watching. An old endpoint that nobody uses is still an endpoint that accepts requests. An old dependency that only dead code uses is still a dependency that needs updating. Deleting the code removes the surface it exposes.
There is a performance angle too. Less code means faster builds, faster tests, and faster startup times. The gains are small for one deletion, but they compound across a year of regular cleanup.
Make it a habit, not a project
Deletion works best as a habit, not as a quarterly cleanup project. A cleanup project feels like a big effort, and it gets postponed. A habit is small and regular. Delete a little code every week. When you touch a file, remove the dead function sitting next to the one you are changing. When you finish a feature, remove the old code it replaced instead of leaving it commented out.
Commented out code is the worst form of dead code. It cannot run, it cannot be tested, and it still confuses everyone who reads it. If you are not sure whether to delete it, delete it. Version control remembers. A comment that says this code is not used anymore is a confession that it should be gone.
Some teams add deletion to their review checklist. When a reviewer sees a change that replaces old code, they ask whether the old code is gone too. That one question, asked consistently, keeps the codebase from growing without bound.
The code you keep for a legal reason
There is one exception to the delete it rule, and it is worth naming. Sometimes code must be kept because a law, a contract, or a regulator requires it. Old tax calculation logic, for example, may need to stay available for years after it stops being used, so that past records can be reproduced. That is a real reason to keep code, and it should be handled deliberately.
The answer is not to leave the code in the main codebase, confusing everyone who reads it. The answer is to move it somewhere explicit: a separate module, clearly labeled, with a comment explaining why it exists and when it can be removed. If the reason is a legal requirement, write down which requirement and the date it expires. When that date passes, delete the code.
Feature flags deserve the same treatment. A flag that was added to roll out a feature gradually is useful for a few weeks. A flag that is still in the code two years later is dead code with a switch attached. Flags that are fully rolled out should be removed, along with the old code path they guarded. A flag cleanup every quarter keeps the count manageable.
The principle stays the same. Code that exists for a reason should say what the reason is. Code that exists because nobody got around to removing it should be removed. The difference between the two is a sentence of explanation, and that sentence is worth writing.
Write code when you need to. Delete code when you do not. The second half of that sentence is the part most teams forget, and it is the part that keeps a codebase healthy over years instead of months. The best code you will write this week might be the code you remove.