I'm using +[NSUserDefaults standardUserDefaults]
to store application settings. This consists of roughly a dozen string values. Is it possible to delete these values permanently instead of just setting them to a default value?

- 6,759
- 1
- 22
- 13

- 6,607
- 12
- 42
- 49
-
1[**Delete all keys from a NSUserDefaults**](http://stackoverflow.com/questions/6797096/delete-all-keys-from-a-nsuserdefaults-dictionary-iphone) – Hemang Aug 30 '13 at 13:33
15 Answers
You can remove the application's persistent domain like this:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
In Swift 3 and later:
if let bundleID = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundleID)
}
This is similar to the answer by @samvermette but is a little bit cleaner IMO.

- 6,759
- 1
- 22
- 13
-
This worked great, thanks! Just don't forget to release appDomain afterwards. – IcyBlueRose Oct 17 '11 at 01:49
-
16@IcyBlueRose - bundleIdentifier is an autoreleased object since it doesn't begin with init or new, so you would over-release it. – Christopher Rogers Oct 17 '11 at 06:28
-
1Good to know, thank you! But I was talking about the string appDomain. Is that auto released also? – IcyBlueRose Oct 17 '11 at 14:28
-
2@IcyBlueRose The object returned by bundleIdentifier is the same as the object referenced by appDomain. – Christopher Rogers Oct 18 '11 at 05:44
-
Will this undo a previous call to `-[NSUserDefaults registerDefaults:]`? – ma11hew28 Mar 21 '12 at 16:41
-
No, it should not because those defaults are in a separate, volatile (i.e., not persistent) domain at the bottom of the search list. – Christopher Rogers Mar 23 '12 at 10:27
-
@IcyBlueRose Two cases are possible. If you use ARC, then the string appDomain is retained, but taken care of by ARC. If you don't use ARC, then the string appDomain is not retained. Either case means you shouldn't release it yourself. – KPM Aug 01 '12 at 06:02
-
3I can't seem to get this to work on 10.8 any more, could someone please confirm that this works? Here's a related SO question: http://stackoverflow.com/questions/13595415/osx-preferences-file-removepersistentdomainforname-has-different-functionality – DaGaMs Mar 19 '13 at 12:02
This code resets the defaults to the registration domain:
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
In other words, it removeObjectForKey
for every single key you ever registered in that app.
Credits to Ken Thomases on this Apple Developer Forums thread.

- 40,269
- 27
- 112
- 144
-
4Thanks a bunch. Why `[NSUserDefaults resetStandardUserDefaults]` doesn't do this is beyond me. – jakeboxer Jan 24 '11 at 22:52
-
2@jboxer I just spent some time studying the Apple documentation and resetStandardUserDefaults basically flushes the in-memory buffer to disk and wipes it clean. So the next time you try to retrieve a value it has to grab it from disk. Core Data's NSManagedObjectContext also uses similar "reset" terminology. – Christopher Rogers Jul 26 '11 at 02:24
-
2Oops, I meant that it wipes the in-memory buffer without writing it to disk. So any changes you made before synchronizing to disk will be lost. – Christopher Rogers Nov 21 '11 at 07:10
-
6Christopher, I think you have it backwards although maybe things changed. resetStandardUserDefaults is the most confusingly-named call I've seen so far in iOS. The Apple Docs say "Synchronizes any changes made to the shared user defaults object and releases it from memory." so it should really be called flushAndReleaseStandardUserDefaults. I'm taking the time to comment on an old comment because I just got caught by this call and want to avoid anyone else being burned (I now have to tell a client I need to update 90 apps). – Andy Dent Oct 26 '14 at 14:12
Did you try using -removeObjectForKey
?
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];

- 20,427
- 11
- 57
- 70

- 16,646
- 2
- 55
- 81
-
-
5
-
2While I understand this appears to work, why isn't defunctPreference some sort of system defined constant? I'd sure be nervous it would stop working someday in the future. – David H Nov 09 '11 at 19:58
-
Here is the answer in Swift:
let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)

- 2,019
- 2
- 20
- 27
-
8`NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)` one-liner – Grace Huang Oct 30 '15 at 19:16
-
3Or safer: `if let domainName = NSBundle.mainBundle().bundleIdentifier { NSUserDefaults.standardUserDefaults().removePersistentDomainForName(domainName) }` – Valentin Shergin Aug 22 '16 at 22:30
-
1Swift 4 version of Grace's oneliner: `UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)` – Carsten Hagemann Jul 25 '18 at 22:23
If you need it while developing, you can also reset your simulator, deleting all the NSUserDefaults
.
iOS Simulator -> Reset Content and Settings...
Bear in mind that it will also delete all the apps and files on simulator.

- 20,427
- 11
- 57
- 70

- 753
- 10
- 10
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDictionary allKeys]) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

- 1,791
- 1
- 14
- 14
In Swift:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.dictionaryRepresentation().keys.forEach { defaults.removeObjectForKey($0) }

- 19,175
- 22
- 126
- 148
I love extensions when they make the code cleaner:
extension NSUserDefaults {
func clear() {
guard let domainName = NSBundle.mainBundle().bundleIdentifier else {
return
}
self.removePersistentDomainForName(domainName)
}
}
Swift 5
extension UserDefaults {
func clear() {
guard let domainName = Bundle.main.bundleIdentifier else {
return
}
removePersistentDomain(forName: domainName)
synchronize()
}
}

- 5,987
- 8
- 76
- 112

- 7,166
- 2
- 50
- 53
Note: This answer has been updated for Swift too.
What about to have it on one line?
Extending @Christopher Rogers answer – the accepted one.
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
and yes, sometime you may need to synchronize
it,
[[NSUserDefaults standardUserDefaults] synchronize];
I've created a method to do this,
- (void) clearDefaults {
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Swift?
With swift its even more easy.
extension UserDefaults {
class func clean() {
guard let aValidIdentifier = Bundle.main.bundleIdentifier else { return }
standard.removePersistentDomain(forName: aValidIdentifier)
standard.synchronize()
}
}
And usage:
UserDefaults.clean()
I found this:
osascript -e 'tell application "iOS Simulator" to quit'
xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"
Source: https://gist.github.com/ZevEisenberg/5a172662cb576872d1ab

- 15,026
- 20
- 92
- 162
All above answers are very relevant, but if someone still unable to reset the userdefaults for deleted app, then you can reset the content settings of you simulator, and it will work.

- 1,550
- 2
- 21
- 33
It's a bug or whatever but the removePersistentDomainForName
is not working while clearing all the NSUserDefaults
values.
So, better option is that to reset the PersistentDomain
and that you can do via following way:
NSUserDefaults.standardUserDefaults().setPersistentDomain(["":""], forName: NSBundle.mainBundle().bundleIdentifier!)

- 9,404
- 1
- 31
- 57
Expanding on @folse's answer... I believe a more correct implementation would be...
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] persistentDomainForName: appDomain];
for (NSString *key in [defaultsDictionary allKeys]) {
NSLog(@"removing user pref for %@", key);
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
...calling NSUserDefault's persistentDomainForName: method. As the docs state, the method "Returns a dictionary containing the keys and values in the specified persistent domain." Calling dictionaryRepresentation: instead, will return a dictionary that will likely include other settings as it applies to a wider scope.
If you need to filter out any of the values that are to be reset, then iterating over the keys is the way to do it. Obviously, if you want to just nuke all of the prefs for the app without regard, then one of the other methods posted above is the most efficient.

- 649
- 7
- 9
if the application setting needing reset is nsuserdefault for access to microphone (my case), a simple solution is answer from Anthony McCormick (Iphone - How to enable application access to media on the device? - ALAssetsLibraryErrorDomain Code=-3312 "Global denied access").
on the device, go to Settings>General>Reset>Reset Location Warnings
Try This, It's working for me .
Single line of code
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];

- 5,941
- 2
- 44
- 55