Clearing NSUserDefaults

131,131

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.

Share:
131,131

Related videos on Youtube

TonyNeallon
Author by

TonyNeallon

Updated on May 17, 2020

Comments

  • TonyNeallon
    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?

  • TonyNeallon
    TonyNeallon about 15 years
    Cheers sbooth. Much Appreciated.
  • samvermette
    samvermette almost 14 years
    Any way to remove object for all existing keys?
  • jakeboxer
    jakeboxer over 13 years
    Thanks a bunch. Why [NSUserDefaults resetStandardUserDefaults] doesn't do this is beyond me.
  • Christopher Rogers
    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
    IcyBlueRose over 12 years
    This worked great, thanks! Just don't forget to release appDomain afterwards.
  • Christopher Rogers
    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
    IcyBlueRose over 12 years
    Good to know, thank you! But I was talking about the string appDomain. Is that auto released also?
  • Christopher Rogers
    Christopher Rogers over 12 years
    @IcyBlueRose The object returned by bundleIdentifier is the same as the object referenced by appDomain.
  • David H
    David H over 12 years
    While 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
    Christopher Rogers over 12 years
    Oops, 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
    ma11hew28 about 12 years
    Will this undo a previous call to -[NSUserDefaults registerDefaults:]?
  • Christopher Rogers
    Christopher Rogers about 12 years
    No, it should not because those defaults are in a separate, volatile (i.e., not persistent) domain at the bottom of the search list.
  • KPM
    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
    DaGaMs about 11 years
    I 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
    Andy Dent over 9 years
    Christopher, 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.
    Travis M. about 9 years
    Do you have to synchronize after?
  • Grace Huang
    Grace Huang over 8 years
    NSUserDefaults.standardUserDefaults().removePersistentDomain‌​ForName(NSBundle.mai‌​nBundle().bundleIden‌​tifier!) one-liner
  • Valentin Shergin
    Valentin Shergin over 7 years
    Or safer: if let domainName = NSBundle.mainBundle().bundleIdentifier { NSUserDefaults.standardUserDefaults().removePersistentDomain‌​ForName(domainName) }
  • Carsten Hagemann
    Carsten Hagemann almost 6 years
    Swift 4 version of Grace's oneliner: UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)