# How to delete company with custom code workflow

**URL:** <https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414>\
**Category:** Data Hub\
**Created:** [November 6, 2023, 11:54pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414 "2023-11-06T23:54:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JJensen\_Aurora](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@JJensen\_Aurora](https://community.hubspot.com/u/JJensen_Aurora)\
**Post date:** [November 6, 2023, 11:54pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414/1 "2023-11-06T23:54:33Z")

</div>

Hi all,  
My company just procured Ops Hub, and I’d like to use the Custom Code step to automatically delete companies where **salesforcedeleted** = **TRUE.**

The problem is I have no idea where to begin with the custom code step! Can someone please help me draft the snippet that would accomplish this task?  
Thank you!

---

<div class="post-metadata">

**Author:** ![Berangere](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/berangere/32/220784_2.png) [@Berangere](https://community.hubspot.com/u/Berangere)\
**Post date:** [November 7, 2023, 3:21pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414/2 "2023-11-07T15:21:24Z")

</div>

Hi [@JJensen\_Aurora](https://community.hubspot.com/t5/user/viewprofilepage/user-id/511336),  
Congratulations on acquiring the Operations Hub 🎉 and thank you for reaching out to the Community!  
I’d like to ask some of our Community Members who used custom code: Hi @jjones41, @Sawdog and @asgJim did you end up using custom code? If yes, could you please share some insights with [@JJensen\_Aurora](https://community.hubspot.com/t5/user/viewprofilepage/user-id/511336), please?  
I’d like to also invite some of our top experts on this subject: Hello @louischausse, @Teun and @ChrisoKlepke do you have suggestions to help [@JJensen\_Aurora](https://community.hubspot.com/t5/user/viewprofilepage/user-id/511336), please?  
**If anybody else has anything to add and/or share, please feel free to join in the conversation 🙂**  
Thank you very much 🌟 and have a wonderful day!  
Best,  
Bérangère

---

<div class="post-metadata">

**Author:** ![louischausse](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/louischausse/32/18067_2.png) [@louischausse](https://community.hubspot.com/u/louischausse)\
**Post date:** [November 7, 2023, 3:48pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414/3 "2023-11-07T15:48:25Z")

</div>

Hi [@JJensen\_Aurora](https://community.hubspot.com/t5/user/viewprofilepage/user-id/511336)  
Here’s how to do it using NodeJS  
1 - Create a private app with the scope “crm.objects.companies.write”: [Legacy private apps - HubSpot docs](https://developers.hubspot.com/docs/api/private-apps)  
2 - Create a company-based workflow with the triggers to meet **salesforcedeleted** = **TRUE** and add a “custom coded action” where you want the company to be deleted  
3 - Create a secret with the token of the private app you created at step 1: [Workflows | Custom Code Actions - HubSpot docs](https://developers.hubspot.com/docs/api/workflows/custom-code-actions#:~:text=To%20add%20a%20new,future%20custom%20code%20actions).  
In the code below the name of the secret is OPS\_TOKEN but you can name it whatever you want, but if you do name it differently you will need to match it in the code.  
4 - Select your secret:

 ![louischausse_1-1699372327041.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/7/2792feac2ad22807ce3b0d2488bb3ca8ab62be04.png)

5 - Select record if in the inputs:

 ![louischausse_0-1699372043357.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/5/55fe55b2b2f382ca0823d9fdc68866520d872f12.png)

6 - Replace the code with this code:

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event) => {

 const token = process.env.OPS_TOKEN

 const hubspotClient = new hubspot.Client({"accessToken":token});

 const companyId = event.inputFields['hs_object_id'];

 try {
 const apiResponse = await hubspotClient.crm.companies.basicApi.archive(companyId);
 console.log(JSON.stringify(apiResponse, null, 2));
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
 }
}

```

7 - Save and Ta-dam!  
Don’t forget to mark my reply as a solution if you are satisfied. If not, do not hesitate to ask me anything!  
Sincerely yours,  
The Wizard of Hubs 🧙‍

---

<div class="post-metadata">

**Author:** ![JJensen\_Aurora](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@JJensen\_Aurora](https://community.hubspot.com/u/JJensen_Aurora)\
**Post date:** [November 7, 2023, 8:05pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414/4 "2023-11-07T20:05:58Z")

</div>

Wow amazing Louis thank you so much!!  
Not only for the solution but also answering in a way that helps me understand the moving pieces here, for the next time I need to create a custom code action.  
⚠I did hit some rate-limiting errors with the initial enrollment of companies, but removing the try/catch clause based on [this thread’s advice](https://community.hubspot.com/t5/9881-Operations-Hub/API-Rate-Limiting-During-Initial-Workflow-Enrollment/td-p/420973)resolved that quite easily.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![louischausse](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/louischausse/32/18067_2.png) [@louischausse](https://community.hubspot.com/u/louischausse)\
**Post date:** [November 7, 2023, 8:17pm UTC](https://community.hubspot.com/t/how-to-delete-company-with-custom-code-workflow/93414/5 "2023-11-07T20:17:18Z")

</div>

Oh sorry. You’re right I didn’t take into account that you would have many records to enroll at first. I’m glad you figured it out.
