My Sync Code Wiped Local Data on Account Switch — The "Server Is the Source of Truth" Pitfall — Build Log #9
I deleted my account in my own to-do app, signed back in with the same Google login, and every task was gone. The culprit was an account-switch guard in my sync code that cleared the local cache because "the server has the real copy" — on a brand-new account where the server had nothing. Then I found the Supabase free plan had no backups.
On July 11, I lost every task in my own to-do app. Not a user's data — mine. I was the developer, the only victim, and the cause was a piece of "safety" code I had written myself.
This post is the full incident write-up: what the sync code did, why the usual "the server is the source of truth" reasoning failed, how I got most of the data back without any server backups, and what I changed so that a misfiring guard can no longer destroy anything.
TL;DR
- My app keeps a copy of your data on the device and syncs it with a Supabase backend. At the start of sync, a guard cleared the local data whenever the owner ID stored on the device differed from the signed-in user ID.
- That is only safe if the server really holds a complete copy. After I deleted and recreated my account, the same email came back with a new user ID and an empty server side. The guard wiped the one copy that still existed.
- The Supabase free plan has no automatic daily backups and no point-in-time recovery, and the old account's rows were gone too. I recovered from a manual export I happened to have, plus Google Calendar.
- The fix: stash before clearing, never just clear, and run my own daily database dumps.
What I was doing when it happened
I was testing login and account behavior in my iOS app: creating test accounts, deleting them, switching between them. At some point in that loop I also deleted my own everyday account, recreated it with the same Google sign-in, and logged back in.
The app opened to an empty screen. No tasks, no lists, no notes. Everything I had been using the app for, day to day, was gone.
No end-user data was affected. But the more I traced the cause, the worse it looked, because nothing about it was specific to me.
The guard I wrote to protect users
The cause took minutes to find. At the very beginning of the sync routine, I had an account-switch guard. In plain terms:
If the owner ID saved on this device does not match the user who is signed in now, clear all local data on the device.
The intent is sound. If someone signs in on a phone that was previously used with another account, they must not see the previous person's tasks. So when the account changes, you clear the local cache and pull the new user's data from the server.
That design rests on one assumption: clearing is harmless, because the server holds the real copy and we can fetch it again. The device is a cache; the server is the source of truth.
That assumption was never checked in code. It was just believed. And this was the one time it wasn't true.
Why "the server is the source of truth" failed
When you delete an account and create it again, the same email address does not give you the same user. The authentication system issues a new internal user ID. My ID before deletion and my ID after recreation were different values, and nothing on the server linked the new one to the old data.
Here is the sequence that played out:
- I sign in. The app now sees the new user ID.
- The owner ID stored on the device is the old one. They don't match.
- The guard fires and clears all local data on the device.
- Sync tries to fetch the user's data from the server. The new user ID has nothing on the server — the account was created a moment ago.
- Device: empty. Server: empty.
Worse, I had been switching accounts again and again while testing, so this guard ran every time I switched, trimming whatever was left locally each time.
The general lesson is simple and uncomfortable: "the server is the source of truth" does not hold when the server is empty. My code never asked whether the server actually had this user's data before throwing away the local copy. It destroyed first and assumed second.
Then I found out there were no backups
My next thought was, "Fine, I'll restore from last night's database backup."
There was no such backup. I was on the Supabase free plan, and according to the official docs, daily automatic backups only run for paid plans (Pro, Team and Enterprise), and point-in-time recovery is a paid add-on on top of those. The docs advise free-plan projects to export their data regularly themselves (Supabase Docs: Database Backups). My project's backup list was empty.
I also checked whether the old account's rows were still sitting in the database, orphaned. They weren't; nothing tied to the old user ID remained, and even its profile row was gone. There was no route back through the server at all.
That was the moment I understood that, in practice, nobody had been protecting this data. Not the platform, and not me.
How I got the data back
In the end I recovered almost everything, through two routes I had not planned as backups.
1. A manual export I'd taken weeks earlier
On June 24, for no particular reason, I had exported my data from the server to a JSON file on my PC. It was still there: 152 tasks (51 of them unfinished), 5 lists and 39 notes. I re-inserted all of it under the new user ID.
2. Google Calendar, which had quietly become a second copy
The app writes tasks that have a date and time to a dedicated Google Calendar. I had built that as a convenience feature, but it meant the calendar was holding an independent copy of my timed tasks. I pulled the 12 unfinished timed tasks from that calendar; 11 of them had been created after the June 24 export, so I added those back.
Final state: 163 tasks, 5 lists, 39 notes. My settings came back on their own through device sync.
What did not come back: tasks without a time that I had created between June 24 and July 11. Those never went to the calendar, there was no other backup of the device, and so that gap is permanent.
The fix: stash instead of wipe
I made two permanent changes the same day.
1. The guard no longer destroys anything
I rewrote the account-switch logic so that clearing is always preceded by stashing. Before the local data is cleared, it is saved to a separate local slot keyed by its owner ID. Then:
- No owner recorded on the device → don't clear anything; adopt the existing data as-is.
- A genuinely different user signed in → stash the current data under the previous owner's ID, then start fresh (or restore that user's own stash if one exists).
- The original owner comes back → restore their stash.
A simplified sketch of the new flow:
// Simplified sketch, not the exact production code
async function onSyncStart(signedInUserId: string) {
const owner = await getStoredOwnerId();
if (owner === null) {
// Nothing to protect against: keep local data and claim it
await setStoredOwnerId(signedInUserId);
} else if (owner !== signedInUserId) {
await stashLocalData(owner); // save before clearing, always
await clearLocalData();
await restoreStashIfAny(signedInUserId); // same user returning? bring it back
await setStoredOwnerId(signedInUserId);
}
await sync();
}
The point of this change is that if the guard fires by mistake, nothing is lost. The other user's data still never shows up on screen, so the privacy goal of the original guard is kept. There was never any requirement for that guard to be destructive; I had just written it the quickest way.
One more lesson from the rollout: I shipped the fix that night as an over-the-air update, but at first it only reached the test build. The version already on the App Store still ran the old guard at that point. A fix sitting in the repository is not yet a fix in users' hands.
2. My own daily backups
Since the plan I was on didn't back anything up, I wrote a script that dumps every table in the database to JSON, keeps 30 days of dumps, and runs every day at 3:47 a.m. as a scheduled task on my PC (set to run as soon as possible if the PC was off at that time). I triggered it manually once to confirm it actually produced a dump before trusting it.
"A manual export happened to still exist" is luck, not a recovery plan. Now there is a plan.
Still on my list
The account deletion flow itself needs work: re-authentication before deleting, an export step, a grace period before data is really removed, and a clear confirmation. Deleting an account should never be something you can do casually in the middle of testing.
Takeaways for anyone building sync
- "The real copy is on the server" only holds after you have checked that the server is not empty. Never destroy local data based on an assumption you haven't verified in code.
- A deleted-and-recreated account is a different user. The same email address does not mean the same user ID. Don't key data ownership on assumptions about identity that your auth provider doesn't make.
- Before you write a delete, ask whether it can be a stash. If the action is reversible, a misfire is an annoyance instead of an incident.
- Don't assume your plan includes backups. Read what you are actually paying for — or not paying for — and if the answer is "nothing," schedule your own dumps.
- An unintended second copy can save you. Here it was the calendar integration. It's worth knowing where copies of your data already live.
The part that still bothers me most is that this came from code written for safety. The routine meant to protect users' data was the one reliably deleting it. The more defensive a piece of code is, the harder you should question the conditions under which it fires.
The app in this post
Everything above is from building THE TASK: AI To-Do Prep (App Store, free), an iOS app I develop solo. The idea is that AI fills a written task out until it's actually actionable. The rest of the series lives in the build log.
Related reading
Sources
- Supabase Docs: Database Backups — daily backups for Pro, Team and Enterprise plans, point-in-time recovery as a paid add-on, free-plan projects advised to export their data regularly
Comments (0)
No comments yet. Be the first to leave one.