Clearing NSUserDefaults

TonyNeallon picture TonyNeallon · Feb 13, 2009 · Viewed 127.8k times · Source

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?

Answer

Christopher Rogers picture Christopher Rogers · Jul 26, 2011

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.