matti peterbull
matti peterbull

Reputation: 195

how to get this DatabaseReference auto generate ID to remove whole DatabaseReference from client side

enter image description here

let's say, I want to remove the whole collection/ DatabaseReference which is named -MNl6bPhPg6BNnM9NKEy, currently as I understand, with the method:

final roomId = databaseReference.child('/message/$room').push();
print($roomId);

but I got an Instance of 'DatabaseReference', so could you plz first tell me how to set this DatabaseReference by myself instead automatically? And how to remove this DatabaseReference?

Thank you a lot!

Upvotes: 1

Views: 209

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

In order to delete a node, you need to know the entire path to that node. You can then create a DatabaseReference to that path, and call remove() on that reference.

.push() generate a new, unique reference, so roomId will always point to a non-existing node.

So in your case that'd be:

final roomRef = databaseReference.child('/message/-MNl6bPhPg6BNnM9NKEy');
roomRef.remove();

Typically you should already know the key of the room to delete, as you likely loaded the data from there into your application. But if you don't know the key, but do know something else that unique identifies the node, you can use a query to find the node.

Upvotes: 1

Related Questions