How to delete a data from Android Cloud Firestore Database

We are going to learn how to delete a data from Android Cloud Firestore database.

In my previous android code example, we explored how to add data to Android Cloud Firestore database.

This is the time to also learn how to delete a data we have added or someone added in Firestore database.

If you have not used Cloud Firestore database in your android project before, I will suggest you first go through my tutorial on how to add Cloud Firestore database in android project.

We are going to follow these simple steps

i. Create a Firebase project for your app

ii. Add Cloud Firestore database to your project

iii. In your activity layout file, add a Button view. When the button is clicked, a record will be deleted from our Firestore database.

How Cloud Firestore data deletion works

Similar to other databases, deletion in Cloud Firestore database uses a unique id, field or property to identify the record to be deleted.

In our example code below. We want to delete a record in AndroidView collection with document Id (Post).

mDatabase.collection("AndroidView").document("Post").delete()

CREATE A NEW ANDROID PROJECT

  • Open Android Studio
  • Go to file menu
  • Select  new
  • Enter project name
  • Enter activity name
  • Select Default Activity
  • Click on finish button to create a new android project

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:background="#CACACA">


    <Button
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_centerInParent="true"
        android:textColor="#ffffff"
        android:text="Delete"/>

</RelativeLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class FirestoreActivity3 extends AppCompatActivity {
    private FirebaseFirestore mDatabase;
    private Button delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firestore3);
        delete = (Button)findViewById(R.id.delete);
        mDatabase = FirebaseFirestore.getInstance();

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDocument();
            }
        });
    }

    private void deleteDocument(){
        mDatabase.collection("AndroidView").document("Post").delete()
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(FirestoreActivity3.this,"Document Deleted",Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(FirestoreActivity3.this,e.getMessage(),Toast.LENGTH_LONG).show();
                        Log.d("Androidview", e.getMessage());
                    }
                });
    }
}

Add Dependencies

app/build.gradle

implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-firestore:19.0.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment