Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - How to delete many entries at once?

How do I delete all entries for the given push ID?

For example, let's say KoxoxwTqfb50E1Gvi9F push ID is in many locations of my database i.e. under many keys and I want to delete all entries for KoxoxwTqfb50E1Gvi9F at once as opposed to statically deleting all entries (since I know their locations).

In other words, is there a way to tell Firebase "delete all entries for KoxoxwTqfb50E1Gvi9F across the entire database"?

like image 547
Camille Avatar asked Jul 24 '17 15:07

Camille


People also ask

How do I delete multiple records on firestore?

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.

How do I delete all data from Firebase?

The simplest way for deleting data is to call removeValue() on a reference to the location of that data. We can also delete data by specifying null as the value for another write operation such as setValue() or updateChildren().

What does getKey () do firebase?

getKey() returns the key (last part of the path) of the location of the Snapshot. getReference() returns the Reference for the location that generated this Snapshot. getValue() returns the data contained in this Snapshot. hasChild() returns true if the specified child path has (non-null) data.


1 Answers

In order to delete multiple entries from your database, you need to know all those locations (refernces). So with other words, in the way you add data, you should also delete it.

Assuming your database looks like this:

Firebase-root
   |
   --- Users
   |     |
   |     --- userUid1
   |     |      |
   |     |      --- //user1 data
   |     |
   |     --- userUid2
   |            |
   |            --- //user2 data
   |
   --- Groups
         |
         --- groupId1
         |      |
         |      --- //group1 data
         |      |
         |      --- Users
         |            |
         |            --- userUid1: true
         |            |
         |            --- userUid3: true
         |
         --- groupId2
                |
                --- //group2 data

I suggest you using the method below:

private static void deleteUser(String userId, String groupId) {
    Map<String, Object> map = new HashMap<>();
    map.put("/Users/" + userId + "/", null);
    map.put("/Groups/" + groupId + "/Users/" + userId + "/", new HashMap<>().put(userId, null));
    //other locations
    databaseReference.updateChildren(map);
}

This method atomically deletes all those entries. Using these paths, you can perform simultaneous updates to multiple locations in the JSON tree with a single call to deleteUser() method. Simultaneous deletes made this way are atomic: either all updates succeed or all updates fail.

Hope it helps.

like image 146
Alex Mamo Avatar answered Nov 27 '22 14:11

Alex Mamo