1. Home
  2. Docs
  3. Web
  4. Step by Step Guide
  5. Deleting Data

Deleting Data

Getting Started

We can delete data that has been stored in Flybase by instructing our Flybase reference to remove a document by using the unique _id.

Let’s revisit our example from the previous article to understand how we delete data from Flybase.

To simply read our post data, we can do the following:

// Get a reference to our posts
var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
	
// Attach an asynchronous callback to read the data at our posts reference
ref.on("value", function(snapshot) {
	console.log( "we found " + snapshot.count() + " posts");
	if( snapshot.count() ){
		snapshot.forEach( function( post ){
			console.log( post.value() );
		});
	}
});

Once you have that data, you can display it, modify it, or delete it. For example, let’s say we want to delete a document with the _id of 1234567:

// Get a reference to our posts
var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
	
// Delete the document, and attach an asynchronous callback to read the deleted data
ref.remove("1234567", function(snapshot) {
	console.log('Deleted Document : ', snapshot.value() );
});

You can also use deleteDocument in place of remove(). These methods are aliased to each other and work the same

Deleting data is pretty straight forward, you can also edit or remove documents inside your Dashboard.

Reserved Events

We covered Reserved events in the reading data article. Reserved events are events that are triggered when certain events happen, such as retrieving documents, adding a new document, changing a document, deleting documents or even when a device connects or disconnects.

These events will always happen, so it’s up to you to decide how you want to listen to them and handle them.

When you delete a document, the removed event will get triggered, and if we are listening for it, then we can take an action.

The removed event is triggered when a document is removed. Normally, it is used along with added and changed. The snapshot passed to the event callback contains the data for the removed document.

In our blog example, we’ll use removed to log a notification about the deleted post to the console:

// Get a reference to our posts
var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
	
// Get the data on a post that has been removed
ref.on("removed", function(snapshot) {
	var post = snapshot.value();
	console.log("The blog post titled '" + post.title + "' has been deleted");
});

Now that we’ve covered deleting data from Flybase, we’re ready for the next article on going offline in Flybase.

Was this article helpful to you? Yes No