sergkk
sergkk

Reputation: 128

Why doesn't FirebaseFirestore throw an exception deleting a non-existing file?

I want to know when FirebaseFirestore does not delete file from remove:

  FirebaseFirestore db = FirebaseFirestore.getInstance();
            FirebaseFirestoreSettings settings = new 
  FirebaseFirestoreSettings.Builder()
                           .setPersistenceEnabled(false)
                            .build();
  db.setFirestoreSettings(settings);

//delet document from Cloud Firestore by documentId (is uniq name of doucument)
    db.collection(COLLECTION_PATH).document(documentId).delete()

I put non-existing id of document but task is isSuccessful. Is it correct behave?

Upvotes: 3

Views: 1200

Answers (2)

user15532790
user15532790

Reputation: 21

There's a new precondition option you can pass to delete calls so it fails if the document doesn't exist. See firebase SDK docs

snap.ref.delete({ exists: true })

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 599601

The task is considered successful when the document no longer exists on the server. That means that the task is also successful if the document doesn't exist by the time your action gets to the server.

So what you're seeing is indeed the expected behavior. If you want to know if the document previously existed, use a transaction that fetches the document first, checks for existence, and then deletes it.

Upvotes: 5

Related Questions