Delete Custom Object Record via API

Is there a way to delete a custom object record using the Node JS SDK? If not, what is the recommended way to delete a custom object record programmatically? I want to delete a custom object record using one of its non-Hubspot properties as an identifier. In other words, I want to do something like this previous topic I posted, except delete the record instead of updating it.

https://community.hubspot.com/t5/APIs-Integrations/Update-Hubspot-Custom-Object-Record-by-Non-hubspot-Custom-Field/td-p/830460

Hi @DarMan,

Unfortunately not, when it comes to deleting a record it has to be done using the HubSpot assigned record ID. If you attempt to use the “idProperty” parameter in your DELETE request (or supply an ID that is not recognised) it will return an error similar to the below:

{
 "status": "error",
 "message": "Object not found. objectId are usually numeric.",
 "correlationId": "07c0767e-c460-408f-af0e-6b4c5e234dd5",
 "context": {
 "id": [
 {OBJECT_ID}
 ]
 },
 "category": "OBJECT_NOT_FOUND"
}

With that said, there is really only one way to get around this - unless you decide to store the HubSpot ID in your system. It would be to first search for the custom object using the CRM Search API. In the request below I search for an object with “external_id” set to “X1010” and the response contains it assigned HubSpot ID of “1669847002”. I can then use this when deleting the record.

REQUEST 1: CRM SEARCH TO RETRIEVE OBJECT ID
POST https://api.hubapi.com/crm/v3/objects/2-116144016/search
BODY
{
 "filters": [
 {
 "propertyName": "external_id",
 "operator": "EQ",
 "value": "X1010"
 }
 ]
 }

RESPONSE
{
 "total": 1,
 "results": [
 {
 "id": "1669847002",
 "properties": {
 "hs_createdate": "2023-07-19T08:53:07.950Z",
 "hs_lastmodifieddate": "2023-08-07T07:54:40.587Z",
 "hs_object_id": "1669847002"
 },
 "createdAt": "2023-07-19T08:53:07.950Z",
 "updatedAt": "2023-08-07T07:54:40.587Z",
 "archived": false
 }
 ]
}

REQUEST 2: DELETE OBJECT USING ID
DELETE https://api.hubapi.com/crm/v3/objects/1669847002

Hope this is in some way helpful!

I tried this, and it works as specified. One detail I need to add is that the DELETE query still requires the proper authentication header with Hubspot’s token.

Hey @DarMan,

Glad it worked and yes all your requests will need to include an access token within the headers. I tend to omit them from my posts on the community for security reasons! :sweat_smile: