How to report sale of land with paper gain but real loss

I purchased land 20 years ago, and all these years had expenses: taxes, stormwater fees, fire mitigation,  various dues, etc.

Now I sold it, technically at a higher price, so it seems like a gain.

But in reality, accounting for 20 years of expenses, it's a loss.

However, I do not see any way to write them off - the only expenses that seem to be allowed are the closing costs.

Can anyone clarify please?