Mar
Mar

Reputation: 187

Best way to delete Firestore document that you are unsure if it exists?

I'm using Firestore as the NoSQL database to build an app that needs to let users add as friends, block, ... other users

To block someone, I'll set the blocked values, and then delete the current friendship status (if any), but I find this a bit tricky. Should I first check if the document exists, and just then delete it, or does Firestore achieve this automatically? Would I be wasting time & Firestore operations if I add the extra checks?

fbRef.runBatch { 
    it.delete(userFriendsWith)
    it.delete(blockedUserFriendsWith)
    ...
}

fbRef.runBatch { 
    it.get() {
        ...
        if (document.exists()) {
            it.delete(userFriendsWith)
        }
    }
}

Thanks!

Upvotes: 2

Views: 963

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

Just delete the document. There's no need to read it first, if you don't care what's inside. The delete operation won't fail if the document already doesn't exist.

Upvotes: 10

Related Questions