简体   繁体   中英

How to delete data from a Firebase realtime database using Java?

I have created a small application, that should get data from Firebase realtime database. The problem is, I will delete all data after get the information from the Firebase. I have searched for the solution, but all the solutions I found did not work for me. I hope you can help me with my problem.

class Example {
    
    public static Object data1;

    public static void main(String[] args) throws IOException, InterruptedException {
        FileInputStream serviceAccount = new FileInputStream("./google-services.json");
        FirebaseOptions options = FirebaseOptions.builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://example.firebaseio.com")
                .build();
      
        CountDownLatch latch = new CountDownLatch(2);
        Query reference1 = FirebaseDatabase.getInstance().getReference("/\"UserName\"/"data1");
      
        ref1.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot datasnapshot) {
                data1 = datasnapshot.getValue();
                System.out.println(data1); 
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            };
        });
        
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("/\"UserName\"");
        databaseReference.child("/\"data1\"");
        databaseReference.removeValue();
        System.out.println("Successfully deleted user");
        }

I use these:

//1.
//Remove value from child   
mFirebaseDatabase!!.child(userId!!).removeValue()  
db.makeText(applicationContext, "Successfully deleted user", db.LENGTH_SHORT).show()  
  
//2.
// clear information  
user_val.setText("")  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM