I want to delete all saved shared preferences when the user taps on logout. Are there any ways to do this in a single shot without deleting one by one?
Asked
Active
Viewed 6.6k times
57
7 Answers
94
I use shared_preferences
plugin:
In pubspec.yaml
dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.4.3
And in dart file:
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
...
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.clear();
I think this is what you need

Andrii Turkovskyi
- 27,554
- 16
- 95
- 105
-
1`preferences.clear();` does not seem to be working for me. – Arnold Parge Sep 02 '19 at 19:04
-
1It can be if you're checking it right after `preferences.clear();`. I didn't mention, but it is an asynchronous operation, so, maybe you need to use `await preferences.clear();` and check after it – Andrii Turkovskyi Sep 03 '19 at 06:35
-
I did use `await`. – Arnold Parge Sep 03 '19 at 07:15
-
It's quite strange. I think you'd better create new question with detailed description of your problem and all that you've done to solve it. – Andrii Turkovskyi Sep 03 '19 at 14:12
-
Well that would be marked as duplicate of this question. ♂️ – Arnold Parge Sep 03 '19 at 14:14
-
Not if you already tried correct answer to existing question and it didn't help. Just don't forget to mention this in new question with link to this – Andrii Turkovskyi Sep 03 '19 at 14:50
-
i hae one question, where sharedPreferenecs data is clear automatically? after each app restart? – Apr 23 '20 at 16:42
-
Consider updating answer to "await preferences.clear();" please.. – Learner Aug 17 '20 at 12:11
-
@Learner actually, it doesn't matter in this case if we don't need the result. `clear()` method will be launched without `await` too – Andrii Turkovskyi Aug 18 '20 at 13:13
-
@AndreyTurkovsky true. But when test, sometimes i see mismatches... – Learner Aug 19 '20 at 04:16
41
You can simply use clear()
function with your variable it will clear all the shared preferences.
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.clear();
If you want to remove particular key value from shared preferences with key name you can do it like this way as follows.
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.remove('KeyNameHere');

GaganSailor
- 893
- 2
- 9
- 19
17
try
final pref = await SharedPreferences.getInstance();
await pref.clear();

Sami Kanafani
- 14,244
- 6
- 45
- 41
3
Just try
prefManager = await SharedPreferences.getInstance();
await prefManager.clear();

Nickofthyme
- 3,032
- 23
- 40

shrikanta mazumder
- 311
- 2
- 12
1
To Clear Shared Preferences Data
You should import shared_preferences.dart
write this code into the logout button
onPressed: () { SharedPreferences userData = await SharedPreferences.getInstance(); await userData.clear(); }

Motassam ALHamed
- 11
- 1
1
To clear all shared preferences, try this:
SharedPreferences pref = await SharedPreferences.getInstance();
await pref.clear();
To clear particular key value, try this:
SharedPreferences pref = await SharedPreferences.getInstance();
await pref.remove('KeyNameHere');

Muhammad Sulaiman
- 131
- 2
1
final preferences = await SharedPreferences.getInstance();
await preferences.clear();

Lance Olana
- 154
- 1
- 4
-
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 18 '22 at 00:24