How I Let an AI Refactor My Entire SaaS Codebase in 48 Hours (And It Actually Worked)
How I Let an AI Refactor My Entire SaaS Codebase in 48 Hours (And It Actually Worked)
Last Tuesday at 2am, I was staring at 47,000 lines of spaghetti code thinking "this is fine" while my churn rate quietly climbed to 6.8%. By Thursday night, Claude Code had restructured my entire repository, and I'd spent exactly £0 on developer help.
I know how that sounds. Bear with me.
Product: Analytics Dashboard for Shopify Merchants
Revenue: $10,243 MRR (up from $8,900 last month — that's about £8,100 for my fellow Brits)
What happened next: Genuinely surprised me
The Mess I Built Over 18 Months
When I launched my Shopify analytics tool in January 2023, I wrote every single line myself. No co-founders, no employees, no contractors. Just me, VS Code, and the kind of optimism you only have at 1am when you're convinced that "I'll refactor it later" is a legitimate business strategy.
Spoiler: it's not.
By August 2024, the cracks were impossible to ignore:
- Load times: Dashboard took 4.2 seconds to render. Users were... let's say "vocal" about it on Twitter
- API spaghetti: One endpoint touched 14 different files. I know because I counted. Twice. I was procrastinating
- Churn spike: 6.8% monthly churn, up from 3.2% in Q1. Every percentage point here is roughly $100 MRR walking out the door
- Onboarding drop-off: 41% of trial users never came back after day 3. Forty-one percent. That's not a leaky bucket, that's a bucket with no bottom
The real gut punch? A power user named Marcus emailed me: "Love the product, but it's getting slower every month. What happened?"
What happened was 47 features shipped with zero refactoring. Actually, wait — I should clarify that. I did refactor once. Back in March 2023. For about two hours. Then I got distracted building a CSV export feature that three people asked for. Three.
Classic indie hacker stuff.
Enter Claude Code: The Terminal Agent That Doesn't Need Hand-Holding
I'd seen the Anthropic's Claude Code launch floating around but honestly dismissed it as "another AI wrapper." Then I saw Marc Lou tweet about rebuilding his entire backend in a weekend with it.
Marc doesn't hype garbage. If you've been in the indie hacker community for more than five minutes, you know this. So I dug in.
Here's the thing about Claude Code that's different: it's this agentic coding thing that runs in your terminal. Unlike Copilot — which suggests lines like an overeager pair programmer who's had too much coffee — Claude Code operates at the task level. You describe what you want, and it reads your entire codebase, plans changes, and executes them across multiple files without asking you to hold its hand.
Setup took 12 minutes:
npm install -g @anthropic-ai/claude-code
claude
No API keys to juggle. No VS Code extension conflicts where half your plugins suddenly break. Just a terminal prompt: "What would you like to work on?"
I typed "everything" as a joke.
It didn't laugh.
The 48-Hour Refactor Timeline
Hour 0-4: The Audit That Paid for Itself
I started with one command:
"Audit my entire codebase for performance issues, unused code, and architectural problems. Show me the 10 biggest problems."
Claude Code scanned 47,316 lines across 312 files. Took about 4 minutes.
It returned:
- N+1 queries in 7 API endpoints (costing 1.2s per request — I'd somehow never caught this)
- Unused dependencies: 23 npm packages never imported. Twenty. Three. I'd been shipping those to production for 18 months
- Duplicate utility functions: Same date formatting logic copy-pasted across 6 files like I was getting paid per line
- Missing indexes: 4 database tables without proper indexing
- Client-side rendering: 3 components that should definitely be server-rendered
The audit alone justified the $200 in API credits I burned through. I'd been paying $79/month for a monitoring tool (won't name names, but you probably know the one — rhymes with "DataDog") that never surfaced half of this.
Well... that's complicated. It probably could have surfaced it. I just never configured the damn thing properly. Moving on.
Hour 4-12: The Great Cleanup
I told Claude Code:
"Remove all unused dependencies, consolidate duplicate functions into a shared utils folder, and update all imports."
What happened next was genuinely surreal. My terminal showed Claude Code:
- Deleting 23 packages from
package.json - Moving 47 utility functions into
/lib/utils/ - Updating 89 import statements across 34 files
- Running
npm testafter each batch of changes
I just watched. Occasionally I'd type "continue" when it asked for confirmation on breaking changes — which happened maybe four times total.
Oh wait, I should back up. It actually asked about a lodash removal that would've broken the billing module. I almost auto-approved it. That would've been bad. More on that later.
First milestone hit at Hour 7: The app still worked. All 142 tests passed. Bundle size dropped from 2.7MB to 1.9MB.
I celebrated with cold pizza. Living the dream over here.
Hour 12-24: Database Revenge
The N+1 queries were absolutely destroying performance. Here's what one endpoint looked like:
// BEFORE: 47 separate queries
const orders = await db.orders.findAll({ userId });
for (let order of orders) {
order.items = await db.items.findAll({ orderId: order.id });
for (let item of order.items) {
item.product = await db.products.findByPk(item.productId);
}
}
Forty-seven queries. For one page load. I'm genuinely embarrassed looking at this now.
I gave Claude Code the entire /api/ directory and said:
"Fix all N+1 queries using eager loading. Add database indexes where needed. Show me the query count before and after for each endpoint."
It rewrote 7 endpoints using proper JOINs and .include() statements. Then generated migration files for the missing indexes. I've written maybe three database migrations in my life, and this thing spat out four in about 90 seconds.
Second milestone at Hour 18: Dashboard load time dropped from 4.2s to 1.1s. I said "holy shit" out loud. My cat was unimpressed. My cat is never impressed.
Hour 24-48: The Risky Stuff
Emboldened by success — probably too emboldened — I decided to tackle the architecture. My app was a classic monolithic mess with business logic tangled up in route handlers like Christmas lights after a toddler's been at them.
Claude Code proposed:
- Extract a
services/layer for all business logic - Create a
repositories/layer for database access - Keep
routes/thin (max 20 lines per file) - Move 3 heavy components to server-side rendering
This was terrifying. I was asking an AI to restructure my entire codebase. Users were actively on the platform. I think I stress-ate an entire bag of tortilla chips during hour 26. The big bag. The one that says "family size" but let's be honest, it's for one person having an existential crisis.
I deployed a maintenance window banner ("Scheduled improvements: Back in 2 hours") and let Claude Code run.
Hour 36: Tests started failing.
The services layer had broken authentication middleware. Before I could even parse the error log, Claude Code identified the issue:
"The auth middleware is being imported before the services layer initialises. I need to refactor the import order in app.js. Fixing now."
It fixed itself.
I was just... there. Being the human who presses "approve." It felt weird, honestly. I've been coding for seven years and suddenly I'm the bottleneck on my own project because I can't read error logs fast enough. That's a strange kind of existential crisis.
Hour 44: All tests passed. 142 passing, 0 failing.
Hour 48: I deployed to production. Sweating. Refreshing the dashboard. Watching logs like a hawk.
Nothing broke.
I stared at the ceiling for a while.
The Numbers Don't Lie
Two weeks post-refactor, here's what changed:
| Metric | Before | After | Change |
|---|
| Dashboard load time | 4.2s | 0.9s | -78% |
|---|
| API response time (p95) | 820ms | 210ms | -74% |
|---|
| Monthly churn | 6.8% | 4.1% | -39% |
|---|
| Day-3 retention | 59% | 71% | +20% |
|---|
| Bundle size | 2.7MB | 1.4MB | -48% |
|---|
| Lines of code | 47,316 | 38,920 | -18% |
|---|
Cael Lee
Full-stack developer with 8+ years of experience. Currently building AI-powered developer tools. I've tested 20+ AI API providers and coding assistants.