Is there a way to clear all the PlayerPrefs with names that start with a specific string?

Basically title. I’m making a sandbox game where I’m using PlayerPrefs to store the positions of individual blocks. The player has the option to “delete” any of their worlds, and when this happens, I need to clear out all the PlayerPrefs that start with the name of the world (which is how I keep the different world’s prefs separate). Also note I am working in JavaScript

Hola!

Alright, so after a little research, I’m thinking there is no easy way to grab all of the PlayerPrefs that start with blank. I was initially thinking that if we could manage to get an array of all of the keys in PlayerPrefs, we could then loop through that, find the ones that started with the world name, and delete those. UNFORTUNATELY, there doesn’t appear to be a way to really get all of the keys. The best way might be to somehow keep track of the PlayerPrefs settings you make and then reference that to delete all of the existing settings.


Here’s an interesting idea. Let’s say you have a couple worlds. You’d normally have a list of PlayerPrefs settings like this, right?

PlayerPrefs ["FluffyForest_DeathCount", 7]
PlayerPrefs ["FluffyForest_EnemyCount", 30]
PlayerPrefs ["FluffyForest_TreeCount", 506]

...

PlayerPrefs ["MurkyMarsh_DeathCount", 12]
PlayerPrefs ["MurkyMarsh_EnemyCount", 18]
PlayerPrefs ["MurkyMarsh_LostItems", 3]

WHAT IF, maybe you had a special PlayerPrefs variable to hold the other settings? So, somethin’ like this for FluffyForest here:

PlayerPrefs ["FluffyForest_Settings", 
    "DeathCount, EnemyCount, TreeCount"
]

PlayerPrefs ["FluffyForest_DeathCount", 7]
PlayerPrefs ["FluffyForest_EnemyCount", 30]
PlayerPrefs ["FluffyForest_TreeCount", 506]

With this special PlayerPrefs thing, FluffyForest_Settings, every time we add or remove a new setting for FluffyForest, we can add add or remove the setting names from/to the FluffyForest_Settings variable. So if ya want to remove all of the FluffyForest settings, just grab all of the settings names from the FluffyForest_Settings variable like this:

string targetWorld = "FluffyForest";
string worldSettings = PlayerPrefs.GetString("FluffyForest_Settings", "");

Now with this string of settings, [you can split it][1] to get an array of the settings, and then loop through the array deleting the PlayerPrefs stuff like this:

string[] settings = [Your Split String]; 

for (int i = 0; i < settings.Length; i++) {
    PlayerPrefs.DeleteKey(targetWorld + settings*);*

}

PlayerPrefs.DeleteKey(targetWorld + “_Settings”);
----------
Somethin’ like that. Just an idea I had. Hopefully it kind of makes sense hah. That’s about the best way I can think to solve this at the moment.
[1]: How to split a string into array? - Questions & Answers - Unity Discussions