Clearing NSUserDefaults
Solution 1
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.
Solution 2
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.
Solution 3
Did you try using -removeObjectForKey
?
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];
Solution 4
Here is the answer in Swift:
let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
Solution 5
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.
Related videos on Youtube
TonyNeallon
Updated on May 17, 2020Comments
-
TonyNeallon almost 4 years
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?-
Hemang over 10 years
-
-
TonyNeallon about 15 yearsCheers sbooth. Much Appreciated.
-
samvermette almost 14 yearsAny way to remove object for all existing keys?
-
jakeboxer over 13 yearsThanks a bunch. Why
[NSUserDefaults resetStandardUserDefaults]
doesn't do this is beyond me. -
Christopher Rogers almost 13 years@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.
-
IcyBlueRose over 12 yearsThis worked great, thanks! Just don't forget to release appDomain afterwards.
-
Christopher Rogers over 12 years@IcyBlueRose - bundleIdentifier is an autoreleased object since it doesn't begin with init or new, so you would over-release it.
-
IcyBlueRose over 12 yearsGood to know, thank you! But I was talking about the string appDomain. Is that auto released also?
-
Christopher Rogers over 12 years@IcyBlueRose The object returned by bundleIdentifier is the same as the object referenced by appDomain.
-
David H over 12 yearsWhile 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.
-
Christopher Rogers over 12 yearsOops, 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.
-
ma11hew28 about 12 yearsWill this undo a previous call to
-[NSUserDefaults registerDefaults:]
? -
Christopher Rogers about 12 yearsNo, it should not because those defaults are in a separate, volatile (i.e., not persistent) domain at the bottom of the search list.
-
KPM almost 12 years@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.
-
DaGaMs about 11 yearsI 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: stackoverflow.com/questions/13595415/…
-
Andy Dent over 9 yearsChristopher, 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).
-
Travis M. about 9 yearsDo you have to synchronize after?
-
Grace Huang over 8 years
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)
one-liner -
Valentin Shergin over 7 yearsOr safer:
if let domainName = NSBundle.mainBundle().bundleIdentifier { NSUserDefaults.standardUserDefaults().removePersistentDomainForName(domainName) }
-
Carsten Hagemann almost 6 yearsSwift 4 version of Grace's oneliner:
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)