Open In App

How to Delete Shared Preferences Data in Android?

Last Updated : 16 Feb, 2022
Like Article

In Android, Shared Preferences is a method of storing and fetching primitive data with the help of a key. This is file is stored inside the application in the form of an XML file. It is used to save data of types int, long, string, boolean, etc. In this article, we will show you how you could save and delete data from Shared Preferences in Android. Follow the below steps once the IDE is ready.

Read more on Shared Preferences: Shared Preferences in Android with Example

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView and a Button as shown below.


<?xml version="1.0" encoding="utf-8"?>



Step 3: Working with the MainActivity.kt file


Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.



package org.geeksforgeeks.deletesharedprefata
import android.content.Context
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing
        // elements from layout file
        val mTextView1 = findViewById<TextView>(
        val mButton = findViewById<Button>(
        // Calling Shared Preferences and Editor
        val mSharedPreferences = getPreferences(Context.MODE_PRIVATE)
        val mEditor = mSharedPreferences.edit()
        // Using Editor to put string against a key value
        // KEY        VALUE
        // City        Delhi
        // Country    India
        mEditor.putString("City", "Delhi").putString("Country", "India").apply()
        // Displaying the Shared preferences
        // data in TextView when app launches
        mTextView1.text = "${mSharedPreferences.getString("City", "")}\n${mSharedPreferences.getString("Country", "")}"
        // When button is clicked, value against key "City"
        // is removed and updated Shared preferences
        // data is displayed in the TextView
        mButton.setOnClickListener {
            Toast.makeText(applicationContext, "City removed", Toast.LENGTH_SHORT).show()
            mTextView1.text = "${mSharedPreferences.getString("City", "")}\n${mSharedPreferences.getString("Country", "")}"





You can see that when the app launches, both the values are displayed. When the button is clicked, the value of the key “City” is removed.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads