56

I've added NSUserDefaults data retrieval to my app, which is pretty nice. But for testing I would like to reset all the data I added to the defaults database, so that everything is in the state when the user launches the app the first time.

I tried to call:

[NSUserDefaults resetStandardUserDefaults];

but that doesn't do anything. The defaults are still saved and can be retrieved.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Thanks
  • 40,109
  • 71
  • 208
  • 322
  • 1
    This is also answered here: http://stackoverflow.com/questions/6358737/nsuserdefaults-reset – Matt Jan 17 '12 at 01:48

11 Answers11

108

You want NSUserDefaults removePersistentDomainForName. This will remove all user defaults for the application:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

For more information on the NSUserDefaults class see the Apple docs.

Alternately, if all you are concerned with is data in the iOS Simulator, you can do that via iOS Simulator > Reset Content and Settings.

enter image description here

memmons
  • 40,222
  • 21
  • 149
  • 183
  • 3
    If I get the user defaults with a suite name remove don't wokr... `NSUserDefaults(suiteName: "user_manager_remember_user_default")` why?? – Daniel Gomez Rico Dec 01 '14 at 19:26
  • 1
    @danielgomezrico you need to use your suiteName as appDomain to remove user defaults with suite name – stevo.mit May 16 '16 at 10:30
  • This doesn't work in unit tests. What worked for me was only to pass `@"xctest"` as appDomain instead. – yonix Dec 23 '18 at 23:12
55

The easiest way is to remove the app from the simulator-- just like you'd remove it from a real phone, by tapping (clicking) and holding until the icons start vibrating. That removes all app data, and the next time you install from Xcode it's like the first time.

If you have other app data you need to keep, you have a couple of options.

One way would be to have some debug code that calls removeObjectForKey: on each of your defaults keys.

The other is to find the directory where the simulator copy is installed, and remove the file containing the preferences. Use this to find the app:

ls -ld ~/Library/Application\ Support/iPhone\ Simulator/User/Applications/*/*.app

The full path to your app will contain directory whose name is a UUID. In that directory, look in Library/Preferences for the preferences file. Remove that, and user preferences are gone.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • 2
    For beginners, that path shown has spaces in the directory names escaped with BACKSLASH-SPACE. That hung me up for a minute so I thought I'd call it out. – willc2 May 29 '09 at 11:30
  • 6
    No idea why this received so many up-votes. Deleting the app from the simulator is the nuclear option which is not practical in most scenarios -- like automated testing where you want to run tests many times without having to reinstall the app each time. You can reset it using `NSUserDefaults` -- see my answer: http://stackoverflow.com/a/9672703/257550 – memmons Mar 12 '12 at 18:37
7

You may find out what you have "written" to userdefaults for the app are all inside a file delete this .plist file works:

user name/Library/Preferences/com.theAppYouAreHandling.plist
Jiulong Zhao
  • 1,313
  • 1
  • 15
  • 25
7

In Swift 2.0 the following 1 line will reset all the NSUserDefaults for an app:

NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)

dane
  • 631
  • 6
  • 15
5

Actually, this may not be suitable in every circumstance, but since I keep no information of value in the simulator, I just Reset Content and Settings from the iPhone menu, from within the simulator itself.

mmc
  • 17,354
  • 2
  • 34
  • 52
3

Til Xcode 6 and iOS 8 Simulator the location of the plist file changed.

I found the *.plist in the following directory:

[1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist

Deleting the located file from path [1] manually and the NSUserDefaults are gone.

shim
  • 9,289
  • 12
  • 69
  • 108
sys4tron
  • 51
  • 2
3

Here's the swift version:

let domainName = NSBundle.mainBundle().bundleIdentifier!       
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(domainName)
Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
2

In the simulator top menu:

Simulator -> Reset Content and Settings...
Andrew
  • 36,676
  • 11
  • 141
  • 113
0

You can use removePersistentDomainForName method available with NSUserDefaults Class.

Syntax :

- (void)removePersistentDomainForName:(NSString *)domainName

Example :

NSString *strAppBundleId = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:strAppBundleId];
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
0

If you're doing this in a unittest, you may want to keep the state of your app in the current simulator instead of inadvertently wiping it every time you also run your unittests. One way to do this is to simply hang on to the old values for your app domain in setUp() and then restore them in tearDown():

class MyClass_Tests: XCTestCase {
    static let domainName = Bundle.main.bundleIdentifier!
    static var oldUserDefaults: [String : Any]?

    override class func setUp() {
        super.setUp()
        // Hang onto this so we don't inadvertently wipe the app's state while running tests.
        oldUserDefaults = UserDefaults.standard.persistentDomain(forName: domainName)
    }

    override class func tearDown() {
        // Restore the old values.
        UserDefaults.standard.setPersistentDomain(oldUserDefaults!, forName: domainName)
        super.tearDown()
    }

    override func setUp() {
        super.setUp()
        // Wipe the state for each test.
        UserDefaults.standard.removePersistentDomain(forName: MyClass_Tests.domainName)
    }

    override func tearDown() {
        super.tearDown()
    }
}
qix
  • 7,228
  • 1
  • 55
  • 65
0

You can find UserDefaults in following path in Finder, delete the .plist

~/Users/<USER>/Library/Developer/CoreSimulator/Devices/<DEVICE_ID>/data/Containers/Data/Application/<APP_ID>/Library/Preferences/<APP_BUNDLE_ID>.plist

Path components to replace:

1. <USER> = MAC user name
2. <DEVICE_ID> = Device/Simulator Identifier, e.g., 999271B8-FAA6-41DE-9864-4111F422ED12
3. <APP_ID> = Application identifier, e.g., 69928AEF-BCD5-413A-B06F-BC4A07080D62
4. <APP_BUNDLE_ID> = Your apps bundle identifier, e.g., com.company.appname.plist
Saif
  • 2,678
  • 2
  • 22
  • 38