Search code examples
node.jsgoogle-cloud-datastore

Datastore not deleting an entity


I've been trying to get a simple cloud function to delete an entity in Datastore and for some reason it doesn't work, despite not throwing an error.

My code is fairly simple:

const keyToDelete = datastore.key({
    namespace: 'alerts',
    path: ['alerts', entId]
});
datastore.delete(keyToDelete, (err, apiResp) => {
    if (err) console.log(JSON.stringify(err));
    else {
        console.log('Deleted ', JSON.stringify(apiResp));
    }
});

I've triple checked and the key definitely points to a valid entity, and .delete isn't throwing an error - it's just not updating anything, and the response confirms this saying no indexes have been updated:

{
   "mutationResults": [
      {
         "key": null,
         "version": "1598396828478034",
         "conflictDetected": false
      }
   ],
   "indexUpdates": 0
}

I thought the issue was that the key was returning null, but when I spelled something wrong in the key it threw an error. After some Googling I've also seen a few people that were getting that and the deletions were still working, so I don't think that's the issue.

I'm using this exact same code to delete a different entity in a different cloud function, and that one's working perfectly. The only difference between the two is that one that works doesn't have a namespace in the key, as the entities being deleted are in the default namespace. Surely that's not the issue though?


Solution

  • Got this to work by setting an ID for the entity that was a string and not a number, and which therefore turned it into a name and not an ID. I only did this because I noticed the key was only setting a name, and couldn't figure out a way to change this to ID for some reason. Seems a bit odd as everything else works using the ID, but this works for now.