Delete Sharedpreferences created dynamically

Asked

Viewed 602 times

1

In order not to keep redoing requests that have already been brought from the server I persist them using Sharedpreferences... Only they are created dynamically in my app. example:

// essa key é unica para cada post
// ex: MINHA_KEI_1, depois MINHA_KEI_2, e assim por diante...

SharedPreferences  save = contexto.getSharedPreferences(key, Context.MODE_PRIVATE);
SharedPreferences.Editor saveEdit = save.edit();
Gson gson = new Gson();

String jsonPost = gson.toJson(posts);
saveEdit.putString("POST", jsonPost);

saveEdit.commit();

Only after a while, I want to delete all of them so the hand app gets really big.

How to delete all of them at once, even without knowing the KEY?

I thought about creating a sharedPreferences with the generated Keys, and then go through each and delete... I don’t know if there is another way.

2 answers

3

Hello, to delete Shared preferences just use the following code snippet :

SharedPreferences save = contexto.getSharedPreferences(key, Context.MODE_PRIVATE);
SharedPreferences.Editor saveEdit = save.edit();
saveEdit.clear(); 
saveEdit.commit();

-1

Hello, the official method documentation does not cite any method to remove all sharedPrefferences from your app via code without citing the Shared key.

The only time it cleans completely (and no need for the key) is when the app is removed - but programmatically, I don’t believe there’s a way to clean up as it is done in uninstalling the app.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.