Hello all,
I am working on a custom code workflow that I can share with the community to bulk delete contact/company associations. I needed this, found it was a frustration for many, and decided to build it and share. However, I’m struggling a little.
This code works perfectly for companies with ~50 or so contacts. However, it times out for companies with more contacts.
So I tried to modify to batch delete, but I just can’t get it right. I find the contacts, batch out the IDs, but can’t seem to figure out how to delete them correctly. Any thoughts or help would be much appreciated. Here’s the code as it stands:
import os
import requests
# Function to delete associations between a company and contacts in batches
def delete_associations_in_batches(api_key, company_id, contact_ids, batch_size=10):
headers = {
“Authorization”: f"Bearer {api_key}",
“Content-Type”: “application/json”
}
from_object_type = “company”
to_object_type = “contact”
for i in range(0, len(contact_ids), batch_size):
batch_contact_ids = contact_ids[i:i+batch_size]
batch_payload = []
for contact_id in batch_contact_ids:
batch_payload.append({
“fromObjectId”: company_id,
“toObjectId”: contact_id
})
batch_url = f"https://api.hubapi.com/crm/v4/associations/{from_object_type}/{to_object_type}/batch/archive"
batch_response = requests.post(batch_url, headers=headers, json=batch_payload)
if batch_response.status_code == 204:
print(f"Associations deleted in batch for company {company_id} and contacts: {batch_contact_ids}“)
else:
print(f"Error deleting associations in batch: {batch_response.status_code}”)
def main(event):
# Retrieve the API key and company ID from environment variables (secrets)
api_key = os.getenv(“Salty_Breakfast_Key”)
company_id = event.get(‘inputFields’).get(‘companyID’)
# API endpoints
company_url = f"https://api.hubapi.com/crm/v3/objects/companies/{company_id}"
association_url = f"https://api.hubapi.com/crm/v3/objects/companies/{company_id}/associations/contacts"
headers = {
“Authorization”: f"Bearer {api_key}“,
“Content-Type”: “application/json”
}
# Retrieve company information
company_response = requests.get(company_url, headers=headers)
if company_response.status_code == 200:
company_data = company_response.json()
company_name = company_data.get(“properties”, {}).get(“name”, “Company Name Not Found”)
print(f"Company Name: {company_name}”)
else:
error_message = f"Error fetching company information: {company_response.status_code}"
print(error_message)
return error_message
# Step 1: Retrieve record IDs of associated contacts
contacts_response = requests.get(association_url, headers=headers)
if contacts_response.status_code == 200:
contacts_data = contacts_response.json()
contact_ids = [assoc[“id”] for assoc in contacts_data.get(“results”, [])]
if contact_ids:
delete_associations_in_batches(api_key, company_id, contact_ids, batch_size=10)
else:
print(“No associated contacts to delete.”)
else:
print(f"Error fetching associated contacts: {contacts_response.status_code}")
# Call the main function
if __name__ == “__main__”:
main(None)