Giới thiệu
Bài viết này hướng dẫn cách xóa dữ liệu cụ thể hoặc toàn bộ dữ liệu trong SharedPreferences của ứng dụng Android.
Các bước thực hiện
Bước 1: Tạo dự án mới
Tạo project Android Studio mới.
Bước 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvBeforeDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Xóa SharedPreference (City)" />
<TextView
android:id="@+id/tvAfterDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
Bước 3: Strings resource (res/values/strings.xml)
<resources>
<string name="app_name">SharedPref Demo</string>
<string name="sharedPref_key_player">player_name</string>
<string name="sharedPref_key_country">country_name</string>
</resources>
Bước 4: Code Java (src/MainActivity.java)
package app.com.sample;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView tvBeforeDelete, tvAfterDelete;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Button btnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBeforeDelete = findViewById(R.id.tvBeforeDelete);
tvAfterDelete = findViewById(R.id.tvAfterDelete);
btnDelete = findViewById(R.id.btnDelete);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
// Lưu dữ liệu mẫu
saveSampleData();
displayData(tvBeforeDelete, "Trước khi xóa");
btnDelete.setOnClickListener(v -> {
// XÓA 1 KEY CỤ THỂ
editor.remove(getString(R.string.sharedPref_key_country));
editor.apply();
// Hoặc xóa TẤT CẢ: editor.clear().apply();
displayData(tvAfterDelete, "Sau khi xóa Country");
});
}
private void saveSampleData() {
editor.putString(getString(R.string.sharedPref_key_player), "Cristiano Ronaldo");
editor.putString(getString(R.string.sharedPref_key_country), "Portugal");
editor.apply();
}
private void displayData(TextView textView, String title) {
String player = sharedPreferences.getString(getString(R.string.sharedPref_key_player), "");
String country = sharedPreferences.getString(getString(R.string.sharedPref_key_country), "");
textView.setText(title + "\nPlayer: " + player + "\nCountry: " + country);
}
}
Các phương thức xóa quan trọng:
editor.remove("key"): Xóa một key cụ thểeditor.clear(): Xóa toàn bộ dữ liệu trong SharedPreferences đó- Luôn gọi
apply()(async) hoặccommit()(sync) sau khi sửa đổi
Bước 5: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Chạy app, quan sát TextView đầu tiên hiển thị cả Player và Country. Nhấn nút xóa, TextView thứ hai chỉ còn Player (Country đã bị xóa).

Best Practices
- Sử dụng
apply()thay vìcommit()để không block UI thread - Kiểm tra key tồn tại trước khi xóa:
sharedPreferences.contains("key") - Để xóa toàn bộ app data: Settings → Apps → Your App → Storage → Clear Data