A simple getting started guide to Firebase Database, a NoSQL database with a rich client side API.
We’ve seen how to create and retrieve data from our Firebase database using some simple code to populate our database with a bunch of salads. So now we will create a shopping cart for our users to demonstrate updating and deleting data in realtime.
Recall the post on Firebase Authentication. Remember Anonymous users? Utilising the idea of anonymous users, your app should always be either logged in as a real user or an anonymous user. This means that you always have access to a Unique User Id.
We will use this value to create a new node in our Firebase database
If you are used to REST, this translates to something like this
Into our cart, we want to put Salads. But we don’t want to copy the entire Salad object into the cart, just the ID of the Salad so that we get this in the Firebase Database console:
Notice the three entries under cart , this corresponds to the app below:
To add and delete from our shopping cart is simple:
This is great for updating our shopping cart, but we also want to watch our shopping cart so that we can make changes on screen to synchronise with these persistent changes. We do this by using the new paradigm in Firebase Database addListener to setup a real time connection to the nodes of our database that are of immediate interest to us.
Here I have created a ValueEventListener to listen for changes to the current user’s shopping cart. All it does is count the number of items in the cart and updates the button on screen.
At first, it seemed a little clunky, a lot of code for simply retrieving a little bit of data. But the cool thing about this ValueEventListener paradigm is the connection from UI to backend is seamless. Put your phone into Airplane Mode, go into the console, delete some entries manually from the shopping cart, take your phone off Airplane Mode, and the listener picks up the change.
Updating and Deleting Data in Firebase Realtime Database
Alright, so here’s the continuation to my last two posts on using Firebase Realtime Database (here is a link to the last one for some reference). So far, we have successfully implemented saving and retrieving data with the use of Firebase Realtime Database in a little bug tracking app. Now, let’s talk about how to update and delete the data!
Updating Data
A new issue ticket looks like this:

By default, the status of a new issue will be set to «Open». We have a «Closed» button to click in order to change the status accordingly. We will set a function to be triggered by a click event on that button in order to change the status to «Closed». Right now, we have the «Closed» button set like this:
Exit fullscreen mode
A click event on this button will call the setStatusClosed() function. We pass in the issue’s ID number so that we can reference it when calling Firebase’s update method. We then can specify which key-value to update, and in this case it will be the issue’s status. It should like something like this:
Exit fullscreen mode
The last two lines of code above are simply to have the DOM display the current database after the specified information has been updated.
Deleting Data
We probably won’t want to keep all of the issue tickets, or we will be stuck with a long list of both open and closed ones. The solution to this would be to enable our «Delete» button to get rid of it. The code for the «Delete» button is as follows:
Exit fullscreen mode
The click event on this button will trigger our deleteIssue() function, where we again will pass in the issue ID number. To delete it, we will call Firebase’s remove() method. We will again clear the current innerHTML of the issuesList in our DOM and call the readIssues() function in order to have the updated data displayed, so our code should look like this:
Exit fullscreen mode
I’ll be honest though, I’m not too sure about the way I’m currently updating the DOM. It just seems a little repetitive, and I’m sure there’s a better way to go about it. If you have any suggestions, please feel free to comment below!
How to delete from firebase realtime database?
I am using Firebase realtime database in Android app, and have data like this: 
How can i delete the record «Apple» (marked in picture)?
According to the docs, to remove an item you call removeValue() on the reference. But to get the reference i require the child id. Because its a random generated id (-KISNx87aYigsH3ILp0D), how to delete it?
![]()
7 Answers 7
If you don’t know the key of the items to remove, you will first need to query the database to determine those keys:
this solved my problem
![]()
If you are using firebase-admin you can simply try this out as
and that works for me.
And also do remember to use async and await syntax.
![]()
![]()
You can use this code :
![]()
Depending on how and why you are deleting the data you can use these:
Assume that images is the directory of your firebase database which you want to clear.
Here images is the parent directory of the database. If you want to clear a nested directory (DCIM) inside the images directory so that you can retain the remaining data in it.
In that scenario you can do like this,
As a response to the query, Please try to use setValue method with null value setValue(null) to clear the data from firebase database
Delete data from Cloud Firestore
The following examples demonstrate how to delete documents, fields, and collections.
Delete documents
To delete a document, use the following language-specific delete() methods:
Web version 9
Use the deleteDoc() method:
Web version 8
Use the delete() method:
Swift
Use the delete() method:
Objective-C
Use the deleteDocumentWithCompletion: method:
Kotlin+KTX
Use the delete() method:
Use the delete() method:
Use the delete() method:
Use the delete() method:
Python
Use the delete() method:
Python
Use the delete() method:
Use the Delete() method:
Node.js
Use the delete() method:
Use the Delete() method:
Use the delete() method:
Unity
Use the DeleteAsync() method:
Use the DeleteAsync() method:
Use the delete() method:
When you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections. You can still access the subcollection documents by reference. For example, you can access the document at path /mycoll/mydoc/mysubcoll/mysubdoc even if you delete the ancestor document at /mycoll/mydoc .
Non-existent ancestor documents appear in the console, but they do not appear in query results and snapshots.
If you want to delete a document and all the documents within its subcollections, you must do so manually. For more information, see Delete Collections.
Delete fields
To delete specific fields from a document, use the following language-specific FieldValue.delete() methods when you update a document:
Web version 9
Use the deleteField() method:
Web version 8
Use the FieldValue.delete() method:
Swift
Use the FieldValue.delete() method:
Objective-C
Use the fieldValueForDelete: method:
Kotlin+KTX
Use the FieldValue.delete() method:
Use the FieldValue.delete() method:
Use the FieldValue.delete() method:
Use the FieldValue.delete() method:
Python
Use the firestore.DELETE_FIELD method:
Python
Use the firestore.DELETE_FIELD method:
Use the FieldValue::Delete() method:
Node.js
Use the FieldValue.delete() method:
Use the firestore.Delete method:
Use the FieldValue::deleteField() method:
Unity
Use the FieldValue.Delete method:
Use the FieldValue.Delete method:
Use the firestore.field_delete method:
Delete collections
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. Repeat the process until you've deleted the entire collection or subcollection.
Deleting a collection requires coordinating an unbounded number of individual delete requests. If you need to delete entire collections, do so only from a trusted server environment. While it is possible to delete a collection from a mobile/web client, doing so has negative security and performance implications.
The snippets below are somewhat simplified and do not deal with error handling, security, deleting subcollections, or maximizing performance. To learn more about one recommended approach to deleting collections in production, see Deleting Collections and Subcollections.