Clearing UserDefaults during macOS app development
While building and testing a macOS app locally, values stored in UserDefaults remain between launches. This includes data stored using the UserDefaults APIs directly or data persisted via the SwiftUI's @AppStorage property wrapper. But it's often useful to clear saved values to test onboarding, restore default settings, and reproduce a fresh app state.
We can quickly clear this saved data directly from Terminal using the defaults command.
To remove all values stored in an app's default domain, run:
defaults delete com.example.MyApp
Replace com.example.MyApp with your app’s bundle identifier.
To remove a specific value, provide the key:
defaults delete com.example.MyApp hasCompletedOnboarding
This can be useful when testing a specific flow without clearing everything.
If the app stores values in a custom suite, use the suite name in place of the bundle identifier.
To remove all stored values from the suite, run the following command replacing group.com.example.shared with your suite name:
defaults delete group.com.example.shared
To remove a specific value from the suite, provide the key after the suite name:
defaults delete group.com.example.shared hasCompletedOnboarding



