'How to remove multiple children in one call - Realtime Firebase
I wan to delete multiple children with known key. I searched all the related posts but it doesn't seem to work. It shows red bracket.
Below is my code which i refer from other posts:
var updates = { }
for(i in removedProductVariationIdList){
updates["user/$i"] = null
}
Screenshot:
Firebase Structure (I want to remove 3, 4 under Product_Variation for example, which i know the key) :
Solution 1:[1]
Java doesn't support template literals with the $i notation in regular strings, as far as I know. The simplest way to build a map with the null values:
Map<String, Object> updates = new HashMap<>();
for(i in removedProductVariationIdList){
updates.put("user"+i, null);
}
Also see:
Solution 2:[2]
You need to declare values as a map.
val map = mutableMapOf<String, Any?>()
Solution 3:[3]
Solution:
With the help from Frank's reference link and danartillaga answers, below is my code in Kotlin which works:
val productVariationRef = FirebaseDatabase.getInstance().getReference("Product_Variation")
val updates = hashMapOf<String, Any?>()
for (i in removedProductVariationIdList){
updates.put("/$i", null)
}
productVariationRef.updateChildren(updates)
Firebase Structure - It will delete all the key (e.g. 3, 4, 5, 8) stored in removedProductVariationIdList together with its children, whole branch dissapears, which is exactly what i want :
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
| Solution 2 | |
| Solution 3 | AhChing11 |



