How to delete UserDefaults data on macOS and Catalyst

⋅ 2 min read ⋅ UserDefaults Catalyst macOS

Table of Contents

UserDefaults is a commonplace to save app preferences and settings. And you might need to delete or reset this many times during development.

In iOS, if you want to delete the app's user defaults, you can simply delete the app. But that is not the case for macOS and Catalyst app.

To delete user defaults on macOS/Catalyst, we need help from the default command.

What is a defaults command

macOS applications and other programs use the defaults system to record user preferences and other information to be maintained when the application isn't running.

When you use UserDefaults in iOS, it is saved in the defaults system when translating to Catalyst.

The defaults command is a way to interact with these values.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

What is a domain

Every user defaults belong to a domain. The defaults delete command need to know a domain to identify the one you want to delete. To domain that your user defaults belong to depends on how you initialize them.

Domain for UserDefaults.standard

When you use standard user defaults, the system uses your app bundle id as a domain.

UserDefaults.standard.set("A", forKey: "a")

Domain for custom UserDefaults

UserDefaults.standard is not the only way to initialize a user defaults. You can initialize user defaults with a custom domain using init?(suiteName suitename: String?).

UserDefaults(suiteName: "Custom")

The suitname will be used as a domain. In this case, the domain is "Custom".

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Commands

After you know how to find the domain, here are two ways you can delete your user defaults.

Removes all information

To delete every key under user defaults, you use defaults delete domain.

defaults delete your.app.bundle.id
// or
defaults delete suit.name

Examples

UserDefaults.standard.set("A", forKey: "a")
// defaults delete com.sarunw.app

UserDefaults(suiteName: "Custom").set("A", forKey: "a")
// defaults delete Custom

Remove a specific key

If you want to delete just one key, you can use defaults delete domain key.

defaults delete your.app.bundle.id key
// or
defaults delete suit.name key

Examples

UserDefaults.standard.set("A", forKey: "a")
// defaults delete com.sarunw.app a

Read more article about UserDefaults, Catalyst, macOS, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to use SwiftUI Picker

Learn how to use Picker, a UIPickerView equivalent for SwiftUI.

Next
How to add a unit test target to a Tuist project

We will see how hard (or easy) it is to add a new unit testing bundle target to your Xcode project with Tuist.

← Home