Suluuboi
Suluuboi

Reputation: 69

Deleting all files in a firebase folder?

The Firebase documentation on deleting data says:

Delete data

The simplest way to delete data is to call remove() on a reference to the location of that data.

You can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.

can anybody explain to me what they mean by the last line I think is could help me delete all files in a firebase folder?

Upvotes: 0

Views: 1136

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If you have this in your database:

users: {
  user9266622: {
    id: 9266622,
    name: "SylvainC"
  },
  user209103: {
    id: 209103,
    name: "Frank van Puffelen"
  }
}

Then you can delete a specific property with something like:

firebase.database().ref("/users/user209103/name").remove();

You can remove an entire branch by calling remove() on the top of that branch. So to remove "my" entire profile:

firebase.database().ref("/users/user209103").remove();

I think this last snippet is what you're looking for: it removes /users/user209103 and everything under it.

The last line of the documentation you quote is about updating/removing multiple branches/properties in distinct locations in one call. For example, say you want to remove the id property from both profiles (since they're redundant). You can do this with:

firebase.database().ref().update({
  "/users/user9266622/id": null,
  "/users/user209103/id": null
});

This is called a multi-location update, since you're updating two locations by their full path.

Upvotes: 1

SylvainC
SylvainC

Reputation: 104

In Firebase:

  • dataRef.remove() is strictly equivalent to dataRef.set(null)
  • dataRef.child('foo').remove() is strictly equivalent to dataRef.update({ foo: null })

That means that setting or updating to null will effectively remove the property.

Updates can be used for removing multiple properties in that way: Consider this example Firebase Object:

exampleData = {
  foo: 'anyValue',
  bar: 'otherValue',
  baz: {
    aProp: true,
    anotherProp: false
  }
}

The following

db.ref('/exampleData').update( { bar: null, 'baz/aProp': null })

will result in the following object:

exampleData = {
  foo: 'anyValue',
  baz: {
    anotherProp: false
  }
}

So you can use one update instead of multiple remove (or set) to delete several properties.

Upvotes: 3

Related Questions