You can "Refit/Rebuild" till the heat death of the universe. Parts have service life, in Voyager, had an episode where they were on a planet changing out the Warp Coils because they went past there service life. Basically, things break/breakdown and need replacement or refurbishment.
This is one thing I never agreed with on Trek.
In the real world, thing fall apart usually because we DESIGN them to do so and promote sales of new items in the long run... even though we CAN design technology which virtually never breaks down and is capable of self-repair or self-maintenance (should it be needed).
In the Federation, the materials in question would be produced synthetically with minimal environmental footprint while having superior properties - for comparison, we designed numerous synthetic materials since 1970-ies that can be produced in sustainable abundance with minimal impact to the environment, and yet, we don't do so mainly because of a system based on profit and cost efficiency (not sustainability, problem solving and technical efficiency).
For warp coils that need servicing/refurbishment after say 6 odd years in the field is a bit preposterous... especially if you take into account we're talking about an organization with over 200 years worth of experience, being in multiple conflicts because of encounters with hostile aliens, dangerous anomalies, etc.
And while yes, ships undergo attacks (which strains them), I don't think they would require a 'refurbishment' in less than a decade because they'd be designed to accommodate for such mishaps - and SF projections can be rather accurate.
Now ships need refits. Lets look at Aircraft carriers, they need a minor refit after each 12-18 month mission, be it just minor maintence, refuel, cleaning etc. and I'm thinking every 10 years needs a Major refit, changing propellers, load bearing items etc.
Yes, but money is a limiting factor in how you design things. The military also has contractors who want to ensure a steady supply of new parts to the military and money to keep themselves in business.
Such limitations wouldn't exist for Starfleet.
This is shown in Trek with the Refit of the Constitution class. There were minor refits after the 5 year missions, new warp nacelles, new bridge, Nav Deflector, etc. but after 20 years, it needed a Major refit.
Now refits cost, not necessarily money, but resources. How much stuff does it cost to refit vs. how much to build a new ship all together.
In the 24th century, it costs nothing but energy since replicators then are able to create matter from energy... and that can be obtained in massive quantities from local stars... and ships could probably harvest solar energy in massive amounts on the go if they need to in order to not strain their anti-matter supplies and could technically manufacture what they need on their own.
We've seen from Discovery that both it and Enterprise fashioned a huge assortment of shuttles and pods in their fight against Control in a short amount of time - which actually further solidifies the notion that starships would technically need to have industrial capabilities onboard to provide relief efforts and service themselves in the long run.
Also, the USS Discovery Calypso is canon, and Disco has been waiting for 1000 years after current events (beyond 32nd century) with Zora evolving on a continuous basis.
As said, things age, break etc. Even the load bearing stress members, hull, etc.
Now there's the upgradeability of the ship, as time goes by, new stuff is invented, and older ships are refitted to update to new standards, but at some point you can't upgrade it without an almost complete rebuild.
Example is the Ohio class sub, there power generation capability's were at the limit and any further upgrades required a bigger reactor, that was impossible, so a new class was built.
What would it take to rip out a warp core and Auxiliaries? Lay new eps conduits?
Technically, you just beam them out of ships and replicate newest state of the art technology in their place.
Also, because of the law of accelerating returns and technical efficiency, new technology will inevitably end up being smaller, far more powerful and efficient.
If anything, you will be left with EXTRA space on say the USS Lakota if you refit it to 24th century standards... similarly, you can beam out old conduits (or basically recycle them into energy and create new state of the art ones)... repeat and rinse for the entire ship.
If automation is doing it... it wouldn't really take long at all because a transporter can easily accommodate large groups of people, or a shuttlecraft with each transporter cycle... and if such upgrades are done at proper facilities (such as starbases and drydocks which are usually in star systems and have access to say huge amount of solar energy from local stars), you basically end up with virtually inexhaustible amounts of energy.
Heck, even without 24th century replicators, molecular or atomic manufacturing from 23rd century could still be easily accomplished and similarly, you can use transporters for beaming the older bits and pieces out.
In the 24th century, you can just beam out and beam in directly the new frame (or bits and pieces) simultaneously if needed - it would only become easier as time goes on.
Starfleet would only need to decide whether its worth to keep upgrading old ships around for longer than a certain amount of time (say past 200 years or so).
New designs will probably offer different versatility yes, but Discovery was also retrofitted with 32nd century technology and brought up their standards.... nearly 1000 years of technological and scientific evolution... well, technically about 800 to be precise because it seems technology just 'stopped' when the Burn occurred (which is ridiculous when you think about what was left of the Federation and just how much of it was left 'unused').
Or its just Starfleet's policy to keep using older ships for about 200 years before completely removing them from active service.
So it gets to a point where a ship just isn't worth refitting/upgrading, however for Colony defense, cargo, Science, etc. the ship would be still usefull, so its given to them. Now some ships are just worn down and not good for anything, and are scrapped.
Perhaps, but I don't think the Trek writers fully thought things through on some things.
Why put ships into the scrapyard?
Given how the Federation operates, EVERYTHING would be salvaged... aka, old ships would be decomposed into base elements and reconstituted into new ones, or they'd be returned into the environment in useful form... heck we can already do so (but just don't do it due to the kind of system we live in which results in less than 10% of stuff we throw out to be recycled - not because it cannot be [because both plastic, and things like old wind turbines, or old solar panels, etc. CAN all be recycled - and not just talking about basics... talking about actual decomposing of matter into base form and reconstituting it into something else).
I mean for a collection of societies which is concerned about health and safety, it wouldn't make sense to leave stuff in the scrapyard just lying around doing nothing. Its a stupid waste of overall resources (yes, even in space).