AI made rewriting from scratch look cheap, but code was never the expensive part of a product. When refactoring still works, when a rewrite is really due, and why the third path, replacing the system piece by piece, usually wins.
- Published
- Reading
- 13 min
- Author
- Yaroslav Tsarenko

Sound familiar? "The code is a mess. It'd be easier to rewrite it from scratch." A few years ago that sentence came with a budget and a timeline, and most founders backed off. In 2026 nobody wants to back off. AI generates screens in an evening, so why not generate the whole product again, properly this time?
Spoiler: code was never the most expensive part of your product. The expensive parts are the data, the unwritten rules, the integrations and your users' habits. AI won't rewrite those, because they aren't in any prompt.
This article is about making the call without emotion: when refactoring still works, when a rewrite is genuinely due, and why the third option, the one rarely mentioned on the first sales call, wins most of the time.
Why a rewrite looks cheap in 2026
Because generating code really has become cheap. In Supabase's State of Startups 2026, a survey of about two thousand startup builders, 61% said AI wrote more than half of their codebase, and 40% said more than three quarters. When code is cheap to produce, it feels cheap to throw away.
Now look at the companies with the biggest budgets. In June 2026 Gartner predicted that more than 70% of mainframe exit projects started this year will fail to deliver their intended benefits. The reason, according to VP analyst Alessandro Galimberti, is overestimating what generative AI can do with legacy code (CIO Dive's coverage). Organizations that can afford the best tools are getting burned by exactly one belief: "AI will port it for us."
And here's my favorite data point. In a randomized study by METR, 16 experienced developers working in large, mature repositories (over a million lines of code on average) took 19% longer on tasks when AI was allowed. Before the study they expected a 24% speedup; afterwards they still believed AI had made them faster. It's a 2025 study and the tools have improved since, but the lesson holds: on a complex, living product, feeling fast and being fast are different things.
So AI made writing code cheaper. It didn't make understanding a product cheaper. And a rewrite is, above all, an exercise in understanding.
Rewrite, refactor, replace piece by piece: what's the difference?
These words get mixed up, so let's pin them down.
| Approach | What happens | The product meanwhile | When you see results |
|---|---|---|---|
| Refactor | Change the structure without changing behavior: split modules, fix the data model, add tests | Keeps running | In days or weeks, gradually |
| Piece-by-piece replacement | A new system grows next to the old one and takes over requests module by module | Keeps running | After the first module |
| Rewrite from scratch | Build a new product, then switch every user over on one day | Old version frozen | At the end, all at once |
| Replace with off-the-shelf | Move to a SaaS or platform instead of custom code | Runs until cutover | After the data migration |
The first two are evolution. The third is a revolution, with all the risk that implies. The fourth deserves an honest look: if your "unique" system is really a standard store or CRM, a ready-made platform may beat any custom code.
Refactor
- The product keeps runningcustomers and revenue don't stop
- Small stepsevery change can be rolled back
- Knowledge staysedge cases are already in the code
Rewrite
- A clean foundationa new data model and structure
- Two systems at oncethe old one needs care until the end
- Value at the endusers see results late
What you're really rewriting
When people say "let's rewrite it," they picture code. But code is only the top layer. Underneath sits everything the product has learned while running.
Here's what you'll have to recreate whether or not anyone asks for it:
- Unwritten rules. That odd
ifstatement that fixes a bug for one customer from 2023. It's in the old code. It's not in the spec. - Data. Years of records, some of which match no current rule, because the rules changed along the way.
- Integrations. Payment providers, shipping, accounting, email, each with its own quirks.
- URLs and SEO. Pages that bring traffic have to keep their URLs or get correct redirects.
- User habits. People are used to their workflow, even a clunky one.
- Operational details. Reports for accounting, an export for the sales manager, manual admin steps only one person knows about.
Back in 2000, Joel Spolsky called rewriting from scratch the single worst strategic mistake a software company can make. His example was Netscape, which went almost three years between version 4.0 and the first beta of 6.0 while losing the market (Joel's essay). His core point: old code looks messy because reading code is harder than writing it, while in fact it's full of fixes discovered through real use.
Does that mean you should never rewrite? No. It means you should count honestly what you're throwing away.
7 signs refactoring still works
If most of these describe you, a rewrite is probably unnecessary.
- The data model is broadly right. The tables and entities describe your business; the code around them is just tangled.
- The problems are local. Three pages are slow, not the whole product. One flow breaks, not everything at once.
- The data is clean. Records reflect reality; there's little junk and few bots.
- Users like the interface. They complain about speed and bugs, not about how the product works.
- The stack is alive. Next.js, Node.js, PostgreSQL: updated, supported, easy to hire for.
- Tests can be added. Even if you have zero today, the key flows can be covered in a week or two.
- The business hasn't changed fundamentally. You sell the same thing to the same people, just more of it.
An example from my own work. On a US B2B sales platform for consumer-goods brands, I didn't rewrite the backend, even though several endpoints were slow. Instead I profiled the slowest PostgreSQL queries with EXPLAIN ANALYZE, added targeted indexes, removed N+1 queries, moved notification dispatch to a job queue with retries, and rebuilt large PDF reports as background jobs. The product never stopped for a day. I walk through how to find those hotspots in my guide to PostgreSQL query optimization.
A refactoring order that doesn't break production
Refactoring can fail too, if it's done chaotically. The order I use:
- Safety net first: a handful of automated tests on the flows that make money (sign-up, ordering, checkout, data access).
- Then measurements: the slowest queries, the most frequent errors, the most expensive calls. Without numbers it's easy to "optimize" something nobody noticed.
- Then the data: database constraints, indexes, fixing the model where it drifted away from the business.
- Only then structure: splitting modules, removing duplication, better names.
Each step is its own release that can be rolled back. Boring? Absolutely. But on Monday the product works the way it did on Friday, just a little better.
5 signs it's time to rewrite
Now the other side. Here refactoring turns into endless patching.
- The data model lies. The product grew from "customer orders" into a marketplace with three roles, but the database still thinks there's one kind of user. Every new feature is a workaround.
- The data is corrupted. Most records are bots, test data or generated junk, and telling real ones apart takes manual work.
- The product is spread across several codebases that duplicate each other and break in turns.
- There's no single authorization layer. You can't prove the product is secure, because every entry point checks permissions its own way, or not at all.
- The platform is dead. The framework or hosting is no longer supported, and security updates have stopped.
That's exactly what AllShip AI, a US freight marketplace, looked like when it reached me. The product lived in three repositories, the database was mostly bots and generated bids, and there was no consistent authorization layer. Refactoring made no sense: there's nothing to refactor when the foundation describes the wrong business.
Even there I didn't throw everything away. Out of 34,921 bot accounts, I migrated only the real people: 16 accounts and 5 carriers, none lost. The new foundation is a single Next.js app with a data model built for three roles, followed by an authorization audit of all 109 entry points.
The second example is more honest, because it's about my own code. I built the Avtoacademy driving school website in 2025 as a Next.js site plus a separate Express server with its own admin. Two repositories, two deploys, and the school couldn't change a line of text without a developer. In 2026 I rewrote it myself into one app with the admin inside. That's sign #3 in action: two codebases where one is enough.
The third path: replace it piece by piece
Most real situations sit in between. The foundation is shaky, but the product earns money, and nobody will let you stop it for six months. For this there's an approach Martin Fowler named the Strangler Fig, after the vine that slowly grows around its host tree.
The idea is simple:
- Put a router in front of the old system, a single entry point for every request.
- Build a new module next to it, say, checkout.
- The router sends that module's requests to the new code; everything else still goes to the old one.
- Repeat until nothing passes through the old system. Then switch it off.
Why does it work? Fowler notes that replacing small components lowers risk and lets the business get value earlier. I'd add three things from practice:
- Every step can be rolled back. The router simply sends requests back to the old module.
- You see progress every week instead of waiting for a "big launch" that always slips.
- Unwritten rules surface gradually. When the new module behaves differently from the old one, you notice it on one module, not across the whole product at once.
- week 1Audit and mapmodules, data, integrations, what hurts most
- week 2Safety nettests on key flows, backups, monitoring
- weeks 2–3Routerone entry point with old and new behind it
- weeks 3–6First modulethe most painful one, real data, rollback ready
- thenModule by moduleuntil nothing goes through the old system
On the web, the router can be as simple as a reverse proxy or rewrites in Next.js: new pages come from the new app, old ones from the old app. Users never see the seam.
Data is the real rewrite
If you remember one thing from this article, make it this: rewrites succeed or fail on data, not on code.
You can redo code. You can't redo a lost customer order, a broken password or a missing payment history. That's why I plan the data migration first, not last:
- Inventory: which tables, how many records, what's real and what's junk.
- Mapping rules: how the old structure lands in the new one, and what happens to records that don't fit the new rules.
- Dry runs: migrate to a copy of the database several times, with a before/after count for each entity.
- Reconciliation: a script that checks totals, counts and relationships after the move.
- Rollback plan: what you do if something goes wrong on cutover day.
At AllShip, the decision not to migrate the bots saved more work than any refactoring could: the new database was honest from day one, and so were the analytics.
What it costs, and the estimation trap
Honestly, nobody can give you an accurate price for a rewrite without an audit. But the cost drivers are well known:
- Data volume and condition. Clean data moves in days; messy data needs hand-written rules.
- Roles and permissions. One kind of user, or customers, providers and admins with different access.
- Integrations. Every external system brings its own edge cases.
- Whether the product can stop. If it can't, part of the budget goes to running two systems in parallel.
- How much knowledge lives only in code. The less documentation, the longer it takes to rediscover the rules.
The chart is schematic, but anyone who's lived through a "big launch" knows the shape. A rewrite from scratch delivers nothing for a long time and often needs more than planned at the end, because the unwritten rules surface at the last minute. Refactoring delivers value steadily but hits a ceiling if the foundation is bad. Piece-by-piece replacement climbs in steps, and every step already works for the business.
The classic trap is comparing "rewrite for X" against "refactor for Y" on the price of the work alone. Compare the full cost: the work, plus keeping the old system alive until cutover, plus the features you won't ship in the meantime, plus migration risk. For what building a new product costs on today's market, see my breakdown of MVP development cost in 2026.
A decision matrix for your situation
Sounds complicated? In practice the decision usually comes down to two questions: how healthy is the foundation (data model, the data itself, access control), and how much does the product need to change soon?
| Your situation | What to do |
|---|---|
| Healthy foundation, product evolving in the same direction | Refactor: tests, indexes, module boundaries |
| Healthy foundation, big changes ahead | Refactor plus piece-by-piece replacement of the modules that change |
| Shaky foundation, product earns money and can't stop | Piece-by-piece replacement with a router and module-by-module migration |
| Shaky foundation, corrupted data, few users | Rewrite: move only the real data into a new foundation |
| Your system is a standard store, CRM or blog | Consider an off-the-shelf platform and keep custom code for what makes you different |
A special case is apps built with Lovable, Bolt or Cursor. The logic is the same, but they have their own typical signals: keys in the browser, open database rules, duplicate payments. I covered those in 10 signs your vibe-coded MVP needs a developer.
What to ask a developer before you agree to a rewrite
A rewrite pitch on the first call isn't always a bad sign, but it's always a reason to ask questions:
- Have you read the code and looked at the data? If not, the recommendation is a preference, not a diagnosis.
- What will you keep from the current product? A good answer names specific modules, screens and data.
- How will the data move, and how will we verify nothing was lost? You want a reconciliation plan, not "we'll run a script."
- When will I see the first working result? Weeks is normal. "Everything at once in six months" is a red flag.
- What happens to the old version meanwhile? Someone has to fix critical bugs in it.
- How will you use AI? The right answer is "for speed, with tests and code review." Why agents rarely finish a task without that is clear from the study where they fully completed just 26% of tasks.
Checklist: 10 questions before you decide
Answer honestly, ideally in writing. It takes half an hour and can save months.
| Question | If "yes" |
|---|---|
| Does the data model describe what the product does today? | A point for refactoring |
| Are more than 80% of database records real? | A point for refactoring |
| Does the product live in one codebase? | A point for refactoring |
| Is the stack updated and supported? | A point for refactoring |
| Are there at least a few tests on key flows, or can you add them within a week? | A point for refactoring |
| Does the product earn money and can't be stopped? | Don't rewrite in one go |
| Will the product do something substantially different a year from now? | A point for piece-by-piece replacement |
| Is access control checked in one place? | If "no", fix that first |
| Do you know every integration and manual process? | If "no", take inventory first |
| Is there a backup someone has actually restored? | If "no", do that before any change |
Five or more points for refactoring, and you almost certainly don't need a rewrite. If the "no" answers dominate, what you need isn't a heroic sprint but a piece-by-piece replacement plan with clear milestones.
What to do this week
Don't decide "rewrite or not" in the heat of the latest outage. Take three steps:
- Go through the checklist above and write the answers down.
- Test your backup by restoring it to a copy. You'll need that in every scenario.
- Get an audit that ends with a written list: what to keep, what to fix, what to replace, and in which order.
A rewrite is a tool, not a rescue. Sometimes you need it, as AllShip did. But most products survive thanks to boring, steady work: tests, data, one module at a time. Less dramatic, sure. But your customers never notice that the foundation under them was replaced.
Was this useful?
– likes · – views
Questions people ask
When should you rewrite an app from scratch?
When the problem is the foundation rather than individual spots: the data model no longer describes what the product does, the data itself is corrupted, the product is split across several codebases, or the platform is no longer supported. Even then, move only the real data and replace the system in stages instead of switching everything off in one day.
Is refactoring cheaper than rewriting?
Usually, yes, because the product keeps running and earning while the risk is split into small steps. A rewrite is expensive less because of the code and more because you have to rediscover every unwritten rule, migrate the data and run two systems for a while.
Can AI rewrite my app in a few days?
AI can generate new code quickly. But a rewrite also has to carry over data, edge cases, integrations and user habits that are in no prompt. In June 2026 Gartner predicted that more than 70% of mainframe exit projects started this year will fail to deliver, precisely because organizations overestimate what generative AI can do.
What is the strangler fig pattern?
An approach where the new system grows next to the old one: a router sits in front of both, and requests move to the new code one module at a time. The old system is switched off once nothing goes through it. The product keeps running the whole time, and every step can be rolled back.
How can I tell if a developer is pushing a rewrite for the wrong reasons?
The red flag is a rewrite recommendation before anyone has read the code or looked at the data. A good contractor starts with an audit, explains what can stay, and shows a plan where the product never stops and the first result is visible in weeks, not months.