Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Code that we no longer use builds up. So what should one do when one doesn't need it anymore?

Deleting it feels wasteful, especially large chunks, and there's always the chance the exact same thing will be needed again later.

Commenting the code out is nice because it leaves it in place for later, but then the code can start to get cluttered.

Version control keeps everything clean and in the right place without the fear of deleting anything, but finding the right code in the version histories is often a project of its own. It's also not great practice to commit code that doesn't work just because you might need it later.

Q: Does anyone have a good solution to managing obsolete code?


migration rejected from Jul 21 '14 at 12:24

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as primarily opinion-based by MichaelT, GlenH7, Bart van Ingen Schenau, gnat, Kilian Foth Jul 21 '14 at 12:24

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

I agree with the others; keep it in version control. You don't want obsolete code hanging around, because later on, when you want to change something and you ask "what depends on this thing?", you don't want all your obsolete code showing up as a dependency that needs to be maintained. – Vanessa MacDougal Sep 13 '11 at 15:03
Not that I particularly care, but I find it interesting that you picked the worst voted answer of the lot. – Craige Sep 13 '11 at 17:07
Old code rarely do what you need. – user1249 Sep 13 '11 at 17:25
Commenting out code is NOT nice. It is a worst practice. – JohnFx Sep 13 '11 at 18:55
Deleting [code] feels wasteful? How long have you been developing? – Jim G. Sep 13 '11 at 19:02

It should be blown away from your current source files. Having commented-out clutter or worse, "unused" code, lingering in your projects imposes a cognitive obstacle course on whoever is working on it. This can really add up in 100+ KLOC sized projects.

If the obsolete code represents a truly major feature set, it will be remembered and folks will be motivated to dig it out of source control when it really matters. Otherwise, most developers actually enjoy starting a feature from scratch every once in a while.


I would create a separate project for the old code (in source control, obviously). This is hard to separate if the dead code is part of a monolithic project that isn't published to repository. That's why I am a fan of modularized, dependent projects continously publishing to a Ivy/Maven repository that is shared publically or across your organisation. If the obsolete code has dependencies on live code then this sort of set-up can help you slice off dead code into a separate project that is published to the repo. Maybe you'll forget about it or maybe you or someone else in your organisation will find a new use for it.

I believe this approach encourages good design with a natural separation of concerns. Having obsolete code bloating these projects unnecessarily increases complexity and decreases code readability for people coming to projects for the first time. As is commenting huge swathes of code, which I'm not a fan of either.


Sounds like you need a better version control tool if you can't easily find what you need. Don't adopt bad practices (like commenting out code) to overcome limitations of the tool.

Keeping historical versions of code (including deleted code) is the job of the source control tool.

Deleting code that no one uses is NOT wasteful. It is obsolete because you found a better way to solve the problem. If that code was worthy of re-use it wouldn't be replaced.


Here's possible things that can be done:

  1. move it to another library/project
  2. comment it out
  3. delete it and store it in version control
  4. keep it there, but ignore it / comment it from public interface
  5. don't do anything to it

When considering obsolete code, it's good to remember that any code change actually has a cost involved, and it might not be worth the effort to even comment it out.


Branch the code out of the current trunk. Delete the old code on the trunk. Optional: Create a second branch for re-use where the code is reviewed for repurposing elsewhere. Commit everything.

This only works for methods which are not public or previously marked depreciated. If public and still current, they need depreciation first. (Different topic.)

No need to branch. Just tag it. You can always branch off the tag later if you need to for some reason. – Adam Jaskiewicz Sep 14 '11 at 16:39

Delete it from your code. Put a tag in your repository so you can find it later.

The more useless code you keep around the more likely you are to have bugs as a result of it.


Obsolete code intertwined with "fresh" production code complicates reading and, therefore, complicates writing of new code. Such code must be kept under control and eliminated as soon as it's no longer needed. Here I wrote a short article on keeping obsolete code under control. Hope it helps.

Please summarise your article here; as it stands this answer feels like little more than a spamvertisement for your blog post. Also see the help center on self-promotion. – Martijn Pieters Jul 18 '14 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.