Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Database doesn't return error when removing a nonexistent key

In the Database I deleted an actual key -LoGeKWGQMJ2EZ5b_Scp which worked fine. But then I tried to delete the same exact key again and it printed successfully deleted. I then added a completely fake key 12345 and it still printed successfully deleted

guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"
    
let postsRef = Database.database().reference().child("posts").child(userId).child(fakeKey)
postsRef.removeValue { (error, _) in
    if let error = error {
        print(error.localizedDescription)
        return
    }

    print("successfully deleted") // always prints
}

I then tried an atomic delete to see what would happen and it printed successfully deleted for 1 real key and 1 fake key. Even after the real key was deleted and I tried again it still printed successfully deleted for both a fake key and a nonexistent key:

guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"

let postsRef = "/posts/\(userId)/\(fakeKey)"

// this is a real key  
let realKey = "-LvXTCmXfpzJA9SUBX8U"  
let postsUserIdsRef = "/posts_userIds/\(userId)/\(realKey)"

var dict = [String: Any]()
dict.updateValue(NSNull(), forKey: postsRef)
dict.updateValue(NSNull(), forKey: postsUserIdsRef)

let rootRef = Database.database().reference()
rootRef.updateChildValues(dict, withCompletionBlock: { (error, _) in
        
    if let error = error {
        print(error.localizedDescription)
        return
    }
        
    print("successfully deleted") // always prints 
})

The odd thing is when I tried to do the same thing with storage I always get a does not exist error, which is what I expect

Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.

code:

// for this example postId is a real key but the videoId is the nonexistent key from above
guard let userId = Auth.auth().currentUser?.uid else { return }
let realPostId = "-MHRkqMuc9c43MDNnbkw"
let nonexistentVideoId = "-LoGeKWGQMJ2EZ5b_Scp"

let storageRef = Storage.storage().reference().child(userId).child(realPostId).child("\(nonexistentVideoId).mp4")
storageRef.delete(completion: { (error) in

    if let error = error {
        print(error.localizedDescription) // prints Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.
        return
    }

    print("successfully deleted") // never prints
})

Btw, with Storage if you successfully delete something once and try to delete it again, it will return a does not exist error.

Why is Database not returning an error for a nonexistent/previously deleted key and/or a fake key but Storage does return an error?

like image 359
Lance Samaria Avatar asked Mar 21 '26 18:03

Lance Samaria


1 Answers

The Firebase Realtime Database considers a delete operation successful when the key doesn't exist after the operation completes. Whether the current operation actually deleted the key is not a factor in its success, so what you're seeing is the intended behavior.

If you want to have an operation that only succeeds when the key was present initially and you then actively removed it, you will need to use a transaction.


Cloud Firestore follows the same logic for delete operations on non-existing documents, so it seems Cloud Storage is the odd one out here. There is no documented reason for this difference, as far as I know.

like image 175
Frank van Puffelen Avatar answered Mar 23 '26 08:03

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!