Fix: Simulator Data Persists Between Builds Preventing Database Schema Updates in Xcode

Fix: Simulator Data Persists Between Builds Preventing Database Schema Updates in Xcode

Written by

in

Quick answer: Use Device > Erase All Content and Settings in the Simulator menu to reset all app data between builds. For automated cleanup, add a build-phase script that deletes the app’s sandbox folder in ~/Library/Developer/CoreSimulator/Devices before each run.

Why this happens

The iOS Simulator preserves each app’s sandbox directory across builds. When your app copies a pre-populated SQLite database into its Documents folder on first launch, that file remains untouched on subsequent runs—even if you change the schema in your Xcode project. The simulator does not automatically wipe the sandbox because it mimics a real device where user data persists across app updates.

Xcode 6 and later store simulator runtimes and device data under ~/Library/Developer/CoreSimulator/Devices/<UDID>/data/Containers/Data/Application/<App-UUID>/. Each build may receive a new container UUID, so the old database file stays behind in the previous container. If you only delete one folder, you miss the others.

This behavior is intentional for testing upgrade scenarios, but it blocks schema migrations during active development when you want a clean slate every time.

How to fix it

1. Use the built-in Erase All Content and Settings (fastest, no code)

Open the Simulator app, then choose Device > Erase All Content and Settings… and confirm. This returns every simulated device to factory state, removing all installed apps, keychains, and user defaults.

  • Works on Xcode 9+ (menu was Simulator > Reset Content and Settings in Xcode 8 and earlier).
  • No project changes required.
  • Clears all simulators, not just the current one.

2. Erase a single device from the command line (scriptable)

Run xcrun simctl erase <device-UDID> to wipe only the targeted simulator. Get the UDID with xcrun simctl list devices.

# Example: erase the booted iPhone 16 simulator
xcrun simctl erase booted

Add this to a pre-action script in your scheme (Product > Scheme > Edit Scheme > Build > Pre-actions) to automate a clean start on every build.

3. Delete only your app’s container in a build-phase script (targeted)

If you want to keep other apps’ data, remove just your bundle identifier’s sandbox before each run. Add a Run Script phase before Compile Sources:

# Remove this app's container from all booted simulators
APP_ID="${PRODUCT_BUNDLE_IDENTIFIER}"
for device in $(xcrun simctl list devices | grep -E 'Booted' | sed -E 's/.*\(([^)]+)\).*/\1/'); do
  xcrun simctl terminate "${device}" "${APP_ID}" 2>/dev/null
  xcrun simctl uninstall "${device}" "${APP_ID}" 2>/dev/null
  # Also wipe any leftover container folders
  find ~/Library/Developer/CoreSimulator/Devices/"${device}"/data/Containers/Data/Application -name "*${APP_ID}*" -type d -exec rm -rf {} + 2>/dev/null
done

The script terminates the app, uninstalls it, and scrubs any orphaned container directories. Adjust the find pattern if your bundle ID contains special characters.

4. Manual Finder cleanup (one-off)

When you only need a quick reset:

  1. Quit the Simulator.
  2. In Finder, press ⌘⇧G and paste ~/Library/Developer/CoreSimulator/Devices.
  3. Locate the device folder (named by UDID), then drill into data/Containers/Data/Application.
  4. Delete the folder whose Library/Preferences/<Bundle-ID>.plist matches your app.

This mirrors the accepted Stack Overflow answer but uses the modern CoreSimulator path.

How to prevent it

  • Add the simctl erase booted pre-action to your Debug scheme so every debug build starts clean.
  • Keep a separate Release scheme without the erase step for upgrade-migration testing.
  • Use NSPersistentContainer with lightweight migration (shouldMigrateStoreAutomatically = true) so schema changes apply automatically on real devices.
  • Version your bundled database (e.g., Model_v2.sqlite) and copy the matching file at launch instead of overwriting a single filename.

FAQ

Does Erase All Content and Settings delete my Xcode project or derived data?

No. It only affects the simulated device’s filesystem (~/Library/Developer/CoreSimulator/Devices). Your project, build folder, and archives remain untouched.

Can I erase just one simulator while keeping others?

Yes. Run xcrun simctl erase <UDID> for a specific device, or right-click a device in Window > Devices and Simulators and choose Erase.

Will the build-phase script slow down incremental builds?

The simctl uninstall call takes ~1–2 seconds on Apple Silicon Macs. For large teams, gate the script with if [ "${CONFIGURATION}" = "Debug" ]; then … fi so Release builds skip it.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.