deleteObject Removes Fields But Doesn't Remove Record

I’m using the deleteObject function in the Javascript library and it seems to delete the contents of the object but the UI still shows that the record exists. Does Algolia eventually remove these empty records for me or do I need to somehow remove them so they don’t accumulate?

When you refer to UI, safe to assume you’re talking about the Algolia Dashboard UI? If so, good…this rules out weird client-side cacheing.

I used the deleteObjects function via the JS library a few months ago and the changes were instant. Full object was removed.

I’m sure an Algolian will chime in soon (I don’t believe they actively monitor the forums on weekends), but in the interim I might suggest focusing on the objectIDs you’re sending over in the deleteObject function. I’d make sure the objectID you’re trying to delete matches the actual objectID on the Algolia side.

Feel free to post a snippet of your deleteObjects code as well as an example record and some of us community members can help you continue to troubleshoot.

Thanks for the reply Chad! I believe the objectID does match since Algolia does clear the specific object that I sent the ID for and none of the others so I’m not sure what the problem is.

For context I’m using Cloud Firestore and set the trigger event to document writes (includes creates, updates, and deletes). Here’s the code:

const ALGOLIA_APP_ID = 'my-add-id';
const ALGOLIA_ADMIN_KEY = 'my-admin-key';

const algoliasearch = require('algoliasearch');
const algolia = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);


exports.updateIndex = (data, event) => {

  const index = algolia.initIndex(event.params.env + "_companies");
  
  if(data.value) {
    data.value["objectID"] = event.params.id
    return index.saveObject(data.value, (err, content) => {
      if(err) { throw err; }
      console.log("Updated index for Company Id:" + event.params.id);
    });
  }
  else
  {
    return index.deleteObject(event.params.id, (err, content) => {
      if(err) { throw err; }
      console.log("Deleted index for Company Id:" + event.params.id);
    });
  }

};

Hi @alex9,

Thanks for contacting Algolia! You asked:

I’m using the deleteObject function in the Javascript library and it seems to delete the contents of the object but the UI still shows that the record exists.

The next step to confirm is whether eventually the object is deleted from Algolia. For example, are they still there now?

I bring this up because all write operations (add, update, delete) are asynchronous by design. This means that a 200 success confirmation means the operation was received and put in a job queue on the Algolia servers, but not that it was completed already.

A step you can take if you would like to wait for confirmation that a job was completed and published is to use waitTask. This is an expensive operation so I would use it sparingly just to confirm your script is working as expected.

For more detail, please feel free to take a look:

@alex9 Hopefully @ajay.david’s suggestion helps resolve your issue, but if not, I might suggest quickly verifying that your sync script is indeed sending the deleteObject command on the related records.

What you describe - record still exists but is now empty - potentially suggests Algolia is receiving a saveObject call with an empty object. If memory serves me right, if Algolia receives saveObject with an objectID that matches an existing record, it replaces the entire existing record with the new object (in this case, an empty one). So perhaps if(data.value) in your code is somehow capturing objectIDs that should be deleted?

You can confirm on the Dashboard > Indices > Logs > Build - then click on any of the records and a modal will pop up. Click on Request body and you’ll see the action received.

Chad I think you nailed it. Now that you mention it this definitely sounds like an empty object is being saved over the existing object rather than deleting it. I’ll verify and report back to confirm.

Cheers!

Update: Confirmed that was definitely the problem. When using the Firestore triggers the saveObject vs deleteObject conditional can’t be based on “if(data.value)”. The trigger event object for a delete sends an empty object rather than a null object so you have to check to see if data.value.fields is present.

1 Like