APIs & Integrations

KBrunette
Member

Cannot delete engagements for contacts via API

SOLVE

I'm using Python and I am attempting to delete all notes/engagements for a contact before re-uploading some new notes for the same contact. I can get the notes for a contact but when I attempt to delete the engagements, I get a 204, but it never deletes the engagements within Hubspot. 

 

Here's my code:

 

def delete_all_contact_notes(self, contact_id):
url = f'https://api.hubapi.com/engagements/v1/engagements/associated/CONTACT/{contact_id}/paged'
response = requests.get(url, headers=self.get_headers())
if response.status_code == 200:
notes = response.json()['results']
for note in notes:
self.delete_note(note['engagement']['id'])
else:
print("Failed to retrieve contact notes.")
return None

def delete_note(self, note_id):

url = f"https://api.hubapi.com/engagements/v1/engagements/{note_id}"

response = requests.delete(url, headers=self.get_headers())
if response.status_code == 204:
print(response.text)
print(f"Note with ID {note_id} deleted successfully.")
else:
print(f"Failed to delete note with ID {note_id}. Error: {response.text}")

 

I've also tried just using a curl command outside of my script:

 

curl -X DELETE -H "Authorization: Bearer <token>" 'https://api.hubapi.com/engagements/v1/engagements/xxxxxxxxx'

 

I don't get a response back, but nothing is deleted. 

 

Any ideas? 

0 Upvotes
1 Accepted solution
KBrunette
Solution
Member

Cannot delete engagements for contacts via API

SOLVE

Got it. Turns out my API call was indeed working, there were just so many of them. Had to update my code to loop through all of them.

 

def delete_all_contact_notes(self, contact_id):
url = f'https://api.hubapi.com/engagements/v1/engagements/associated/contact/{contact_id}/paged'
offset = 0
limit = 100 # Adjust as needed
has_more = True
note_tracker = 0
print("Preparing to delete notes for: " + str(contact_id))
while has_more:
params = {
'limit': limit,
'offset': offset
}

response = requests.get(url, headers=self.get_headers(), params=params)

if response.status_code == 200:

notes = response.json()
print("\tFound: " + str(len(notes)) + " Notes for Hubspot Contact_id: " + str(contact_id))
for note in notes['results']:
note_id = note['engagement']['id']
self.delete_note(note_id)
note_tracker += 1
if notes['hasMore']:
offset += limit
else:
has_more = False
print("\tSuccessfully deleted: " + str(note_tracker) + " for Hubspot Contact ID: " + str(contact_id))
else:
print("Failed to retrieve contact notes.")
return None

View solution in original post

0 Upvotes
3 Replies 3
KBrunette
Solution
Member

Cannot delete engagements for contacts via API

SOLVE

Got it. Turns out my API call was indeed working, there were just so many of them. Had to update my code to loop through all of them.

 

def delete_all_contact_notes(self, contact_id):
url = f'https://api.hubapi.com/engagements/v1/engagements/associated/contact/{contact_id}/paged'
offset = 0
limit = 100 # Adjust as needed
has_more = True
note_tracker = 0
print("Preparing to delete notes for: " + str(contact_id))
while has_more:
params = {
'limit': limit,
'offset': offset
}

response = requests.get(url, headers=self.get_headers(), params=params)

if response.status_code == 200:

notes = response.json()
print("\tFound: " + str(len(notes)) + " Notes for Hubspot Contact_id: " + str(contact_id))
for note in notes['results']:
note_id = note['engagement']['id']
self.delete_note(note_id)
note_tracker += 1
if notes['hasMore']:
offset += limit
else:
has_more = False
print("\tSuccessfully deleted: " + str(note_tracker) + " for Hubspot Contact ID: " + str(contact_id))
else:
print("Failed to retrieve contact notes.")
return None
0 Upvotes
BérangèreL
Community Manager
Community Manager

Cannot delete engagements for contacts via API

SOLVE

Hi @KBrunette, I hope that you are well!

I am delighted to see that all is working for you now!

Also, thanks for sharing this with the Community, it will be useful for many other Community Members!

Have a great day and weekend!

Best,
Bérangère


Saviez-vous que la Communauté est disponible en Français ?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres !

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings!


0 Upvotes
BérangèreL
Community Manager
Community Manager

Cannot delete engagements for contacts via API

SOLVE

Hi @KBrunette,

Thank you for asking the Community!

I understand that you'd like to bulk delete notes (engagements) for contacts via API.

I found this similar post that might help you: "Quick/simple way to delete all Contact Notes for all Contacts?".

I also wanted to invite a couple of subject matter experts to this conversation: Hi @ChrisoKlepke, @himanshurauthan and @SkeeStylus do you have suggestions to help @KBrunette, please?

If anybody else has anything to add and/or share, please feel free to join in the conversation 🙂

Thanks a lot and have a fantastic day!

Best,
Bérangère


Saviez-vous que la Communauté est disponible en Français ?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres !

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings!


0 Upvotes