Skip to content
Advertisement

How to delete a single entry from my Realtime Firebase database in React Native?

I’m not quite sure how to delete a single entry in my Realtime database, each time I run the following, it deletes everything in that database:

const deleteTask = () => {
        console.log('will delete: ', id);
        remove(ref(db, '/todos', id))
    }

I do have the id (can see that with the console.log) and it matches what my firebase shows on the web console. But why is “remove” deleting the entire database?

What makes it strange to me is that this code successfully updates the “done”-status of a single database entry:

const onCheck = (isChecked) => {
        setDone(isChecked);
        update(ref(db, '/todos'), {
            [id]: {
                title,
                done: !doneState,
            },
        });
        console.log('set done/undone')
    };

I had a look at the official Realtime database, but this wouldn’t help me figure out what’s wrong. Same goes for other solutions here on stackoverflow, but most of them have the issue of getting the id, something I already do have.

Any help / tipps are much appreciated! 🙂

Advertisement

Answer

The ref function only takes a single path parameter:

Signature:

export declare function ref(db: Database, path?: string): DatabaseReference;

So the ID you specify as a second parameter isn’t used, so you’re indeed deleting all the todos.

The solution is to call child:

remove(child(ref(db, '/todos'), id))

Or pas in the entire path as one string:

remove(ref(db, '/todos/'+id))
Advertisement