Why dead code feels hard to delete

It’s always surprising when people don’t want to delete dead code because they think they might need it in the future or because they worked so hard on it.
Recently, I had one of those conversations with a colleague. I was trying to convince him that we should delete unused code, and that made me think: why is it that we sometimes feel reluctant to delete dead code?
I think there are a couple of mental models that can help us understand why: the sunk cost fallacy and the loss aversion principle.
Sunk cost fallacy
Code isn’t free. Yes, today LLMs make it cheaper to write code (or at least some type of code). But shipping good features requires planning, coordination, research, and even the coding itself requires us to make many decisions about how to fit it into the existing system. That’s expensive.
But if it’s so expensive, how could we just throw it away?
Because the cost is what we call a sunk cost:
a sunk cost is a sum paid in the past that should no longer be relevant to decisions about the future
As humans, we tend to think that a thing (in this case, code) is valuable because it was costly to create. Especially if we incurred the cost. So, we end up keeping the thing, even if it makes it more costly going forward (that’s why it’s a fallacy).
In reality, we should want to avoid future costs, regardless of what happened in the past. That’s what we should optimize for. Dead code just makes it harder for us to reason about the codebase, and it makes it more costly to introduce new code. That’s why we should remove it.
So rationally, we should remove it. But it still feels bad sometimes. Why is that?
Loss aversion principle
I think the loss aversion principle can help us understand the feeling:
loss aversion is a cognitive bias in which the same situation is perceived as worse if it is framed as a loss, rather than a gain
So, if something seems like a loss, it feels worse. For example, we feel worse if someone gives us $100 and later takes it away than if we had never received the $100 in the first place. In both cases, we end up in the same place, but getting it taken away feels worse. (If you don’t believe me, promise your kid ice cream and then renege on the promise.)
I think the same is true of code.

If I were to tell you that I could add 10k lines of unused code in your codebase instantaneously and completely free, would you accept that?
Of course not!
Why would you want dead code that will make things more difficult for your team in the future? Who cares that it’s free to add today?
But if you think about it, that’s exactly the same thing we’re doing when we keep dead code. We just inverted the scenario.
We feel bad losing the code. But when we’re in our right mind, we wouldn’t accept useless code just because it was free.
A maxim to remember
That’s why I have a maxim:
Code is a liability
Remember that. Code is a liability. It’s not free to create or maintain. The less of it you have, the better. We only need as much of it as is required to serve our customers. Not a tab or space more.