Deleting plist file does not reset app on macOS 10.9+
Asked Answered
M

4

46

While developing a Cocoa application on 10.9, I have noticed that if I go to ~/Library/Preferences and delete the plist file for my app (to reset it), on the next build-and-run, the app behaves as if the plist file had never been deleted at all.

It took me a long time to track down why this happens and I did not see a question/answer about it on SO, so I'm writing this question and answering it myself to help others.

Mcbride answered 10/10, 2013 at 18:54 Comment(3)
A simple solution: restart the computer. Now delete the plist file. Now build and run, and you will be starting with clean prefs. Reason: The prefs are synchronized into memory, and are restored from memory unless you clean out memory first, which is what restarting does.Application
Well, yea, that will do it. But rebooting your computer between each build and run is NOT a practical solution.Mcbride
No one is suggesting rebooting before each build and run - only in the special situation where you want to delete the plist file to reset it. It's a serious issue because you're fighting the system, which is keeping a secret cache of the plist file and just restores it when you delete it.Application
M
62

On 10.9, the system is doing some more robust "caching" of preferences. After deleting the plist file, I fired up Activity Monitor and force-killed the "cfprefsd" process. Be careful: there are multiple processes with this name running and you only want to kill the one running under your own user; do not kill the one running as root.

Doing this seems to flush the preferences cache and on the next run of my app, I get a pristine start-from-scratch launch.

Edit: As reported below, using defaults delete [your bundle identifier] at the command line also appears to eliminate the caching issue. I've had mixed success with this.

Mcbride answered 10/10, 2013 at 18:54 Comment(12)
You can use 'defaults delete' to delete preferences in a cache-friendly way.Arjan
defaults delete "identifier" did clear the .plist file as expected, but not the cache. Killing cfprefsd seemed to workMiddleaged
defaults delete will only work when the preferences system is working normally :) if it doesn't work then, please file a bug, that's not expectedArjan
And by "working normally" might I suggest Catfish_Man means "not having recently mucked about with preference-related processes" - i.e. If you've been killing this process, please reboot to get the machine into a known state, then test to see if you're having problems with 'defaults delete ...'Paper
Shon - are you with Apple? I can promise you, I didn't "muck around" with this process. I used Finder to delete my app's plist file, then wasted an hour of my life trying to figure out why OS X wasn't behaving. Deleting the plist in the Finder SHOULD clear the cached version. That it does not is an oversight on Apple's end. When the user deletes that file, it is ABSOLUTELY CLEAR what his intention is: nuke the prefs for app X.Mcbride
The trouble is that there are roughly 4,549,312 webpages on the Internet that explain to users how to fix "misbehaving" apps. Step #1 is invariably: "Go to ~/Library/Preferences" and delete the app's preference file. This, apparently, no longer works on Mavericks; you must now tell users to go through the terminal and use 'defaults delete'. As far as I'm concerned, that's a bug. cfprefsd should be checking to see if the plist on disk is newer or deleted before it blindly uses its cached copy.Mcbride
If you prefer using the CLI, I just tried "killall cfprefsd" and it seems to work too. Early days still. It's possible there is some new command to make plist replacement a bit easier. And if there isn't I imagine there are plenty of AppleCare employees who will ask Apple engineering to add one.Lynellelynett
I'd recommend against killing cfprefsd because that might also damage prefs other apps were setting recently, I'm afraid. Using the terminal's delete command should be safe and deterministic, on the other hand.Apologist
Unfortunately, the terminal command does not work for me. I can't get a clean start until I kill cfprefsd. I've filed a bug with Apple, but who knows when that'll get fixed.Mcbride
I first tried manually deleting the plist. Then tried with defaults delete MyAppsBundleIdentifier, but it didn't work. Finally what did work was killall cfprefsd.Ahmedahmedabad
Once you've gotten into the "stuff is broken" state by deleting the file, you can't get back out without rebooting or killing cfprefsd. Using defaults helps you not get into that state in the first place.Arjan
The worst problem for me is that I use a "demo" preferences file when taking screenshots of my app. Deleting using the command line works but there is no way I am able to use my "demo" preferences file. My app always launches with a virgin preferences file. I'm not sure what the Preferences folder is anymore.Sociopath
F
15

In terminal:

defaults delete com.somecompany.someapp

Farmhand answered 26/5, 2014 at 20:52 Comment(2)
This works... most of the time. I've still had instances where I couldn't get the app to start clean after using this terminal command.Mcbride
Works great!! Event for reading and writing values! Better than any plist editor ..Latishalatitude
M
14

I found out that killing the user process cfprefsd will reflush the cache, so your changes will be kept

killall -u $USER cfprefsd

Maneating answered 15/12, 2013 at 17:24 Comment(3)
Even though it might appear to work, I don't think that's safe as it might damage recently set prefs of other apps as well. Hence downvoted.Apologist
Oh, sorry, I only now realize that you wrote that the kill does flush its cache. That'd mean that the process is not killed hard but is asked (signalled) to quit, allowing it to perform the flush operation before quitting. In that case, my downvote was unjustified. I cannot revoke it unless you update your answer.Apologist
Thomas' concern is valid, however 'defaults delete' didn't work for me and this did (my use case was replacing the plist manually from a backup). I think the risk is manageable if you know what you are doing.Brig
A
10

BTW, I've just released a GUI app that may be more convenient than working with the defaults command:

http://www.tempel.org/PrefsEditor

It works practically the same as Xcode's plist editor, but affects the user's app preferences directly.

To delete all your prefs, you could open your prefs in my Prefs Editor, Select All, then delete them with the Backspace or Delete key, and they're instantly all gone.

However, for this particular task, using defaults delete might still be quicker, especially if you put the command into a text file ending in ".command", and make it executable (with chmod +x). Then you can double click it from the Finder to execute it.

Apologist answered 14/12, 2013 at 20:50 Comment(1)
That's a wonderful little tool and a great help when debugging things. Thank you for sharing it!Cambrai

© 2022 - 2024 — McMap. All rights reserved.