# Serverless delete rows batch

**URL:** <https://community.hubspot.com/t/serverless-delete-rows-batch/54397>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, professional\
**Created:** [April 5, 2022, 8:45am UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397 "2022-04-05T08:45:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joan1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joan1/32/47092_2.png) [@joan1](https://community.hubspot.com/u/joan1)\
**Post date:** [April 5, 2022, 8:45am UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/1 "2022-04-05T08:45:51Z")

</div>

Hi Everyone, I’m a bit struggle on this point, I want to remove all the row from one table, I already know how to do it one by one but hubspot documentation has a line to remove all the row by providing a list of rows. **POST/cms/v3/hubdb/tables/{tableIdOrName}/rows/draft/batch/purge**

on [Accounts Dashboard | HubSpot](https://developers.hubspot.com/docs/api/cms/hubdb)

I use the serverless function, so I provide the list of the rows I want to remove like that:

’

“69668001298”,“69668001299”,“69668001300”

then I set up the requestBody :

requestBody[‘inputs’] = “69668001298”,“69668001299”,“69668001300”;

I also try when send it to the serverless with the JSON.stringify.

then I call the post:

Requires HubDB table with name donations. Columns: name (text), amount (number)

const API\_HUBDB\_BASE = '[https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxx/rows/draft/batch/purge’;](https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxx/rows/draft/batch/purge)

const DELETE\_TABLE\_API\_URI =

API\_HUBDB\_BASE + ‘?hapikey=’ + apiKey;

Use axios to make a delete request to the API

axios

.post(DELETE\_TABLE\_API\_URI, requestBody)

.then(function(response) {

No matter the way I provide the format I get Invalid input json

Someone got an idea on how to use this function?

Thanks for the help you can provide.

Kind regards.

Joan

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [April 5, 2022, 6:52pm UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/2 "2022-04-05T18:52:05Z")

</div>

@joan1 ,

Would you be able to put the code together so we can see the entirety of it.

Maybe [@Mark\_Ryba](https://community.hubspot.com/t5/user/viewprofilepage/user-id/138856) could help once we see what your code looks like in a more readable way

---

<div class="post-metadata">

**Author:** ![joan1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joan1/32/47092_2.png) [@joan1](https://community.hubspot.com/u/joan1)\
**Post date:** [April 5, 2022, 11:29pm UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/3 "2022-04-05T23:29:47Z")

</div>

Hi @dennisedson ,

Yes, I didn’t send all but keep the main function I used on the different part(vue.js and serverless)  
Vue js part:

mounted(){

let requestRowToDelete =

“[https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows?hapikey=](https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows?hapikey=)” +APIKEY ;

;

axios.all([requestRowToDelete, requestRowToUpdate])

.then(axios.spread((…responses) =\> {

this.dataToDeleteJson= responses[0].data.results;

}

var dataToDeleteString = “”;

```
  for (const dataof this.dataToDeleteJson){

      dataToDeleteString += '"' + data.id + '",';

  }

dataToDeleteString = dataToDeleteString.slice(0, -1));

```

this.dataToDeleteFormatted = dataToDeleteString;

},

methods: {

onUpdate: function(){

```
   const data = {

            "inputs": this.dataToDeleteFormatted

          };

          const requestOptions = {

            'method': 'POST',

            'headers': {

              'Content-Type': "application/json",

            },

            "body": JSON.stringify(data),

          };

      fetch("/\_hcms/api/delete-rows-batch", requestOptions)

          .then(response => response.text())

          .then(result =>{

            var parsedResult = JSON.parse(result);

            console.log(result);

                    }

          })

          .catch(error => console.log('error update', error));

```

},  
serverless.json

{  
“runtime”: “nodejs12.x”,  
“version”: “1.0”,  
“environment”: {},  
“secrets”: [“API\_KEY”],  
“endpoints”: {  
“delete-rows-batch”: {  
“method”: “POST”,  
“file”: “delete-rows-batch.js”  
},  
And finally the delete-rows-batch.js function:

// Require axios library to make API requests  
const axios = require(‘axios’);

// This function is executed when a request is made to the endpoint associated with this file in the serverless.json file  
exports.main = ({ body }, sendResponse) =\> {

const apikey = process.env.API\_KEY;  
let requestBody = {};  
requestBody[“inputs”] = body.inputs;

const API\_HUBDB\_BASE = ‘[https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows/draft/batch/purge](https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows/draft/batch/purge)’;  
const DELETE\_TABLE\_API\_URI =  
API\_HUBDB\_BASE + ‘?hapikey=’ + apiKey;

// Use axios to make a delete request to the API  
axios  
.post(DELETE\_TABLE\_API\_URI, requestBody)  
.then(function(response) {  
console.log(‘HubDB rows deleted’);  
console.log(response.data);  
console.log(response.status);// sendResponse is what you will send back to services hitting your serverless function  
sendResponse({  
body: {  
response: ‘Delete Successful’,  
success: true  
},  
statusCode: 200,  
});  
})  
.catch(function(error) {  
// Handle error  
console.log(‘Failed to delete HubDB rows data’);  
console.log(error.response.data);  
console.log(error.response.status);

sendResponse({  
body: {  
error: error.message,  
success: false  
},  
statusCode: 500 });  
});  
};

---

<div class="post-metadata">

**Author:** ![joan1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joan1/32/47092_2.png) [@joan1](https://community.hubspot.com/u/joan1)\
**Post date:** [April 6, 2022, 4:22am UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/4 "2022-04-06T04:22:30Z")

</div>

My Appologize I didn’t embedded to the code because it seems to be rejected by hubspot control.

---

<div class="post-metadata">

**Author:** ![joan1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joan1/32/47092_2.png) [@joan1](https://community.hubspot.com/u/joan1)\
**Post date:** [April 7, 2022, 8:03am UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/5 "2022-04-07T08:03:00Z")

</div>

Hi @dennisedson ,

So first point, you was right, by spliting the array it can work:  
for (const value of values){

table[count] = value.id

}

and send the table on the value “inputs” to the fetch make the job.

For the apiKey, thanks to mention it, i’m doing a migration from this to serverless function .

kind regards

---

<div class="post-metadata">

**Author:** ![Mark\_Ryba](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mark_ryba/32/22744_2.png) [@Mark\_Ryba](https://community.hubspot.com/u/Mark_Ryba)\
**Post date:** [April 6, 2022, 4:37pm UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/6 "2022-04-06T16:37:51Z")

</div>

There’s a lot going on here, but I _think_ the issue you’re running into is the string actually being sent to the endpoint. It should be a single object with an ‘inputs’ key and an array of values, like this:

```lia-code-sample
{
 "inputs": [
 "12345, 12346, 12347"
 ]
}

```

Also, it looks like you’re using an exposed API key in your Vue module, so you may want to reconsider how that’s being used in conjunction with your serverless functions.

---

<div class="post-metadata">

**Author:** ![joan1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joan1/32/47092_2.png) [@joan1](https://community.hubspot.com/u/joan1)\
**Post date:** [April 6, 2022, 11:30pm UTC](https://community.hubspot.com/t/serverless-delete-rows-batch/54397/7 "2022-04-06T23:30:07Z")

</div>

I see, I will try this morning to set it up as an array of value but if i’m remember well I tried and it was not successfull too.  
I will let you know.
