I am trying to automate the deletion of contacts based on a data extension. I am using this script but it keeps giving me the error "ERROR: Error has occurred".

Any idea what I might be doing wrong?

<script runat=server>

var authEndpoint = 'https://mc7zwyxpz1ynzspkkplqgk0dm798.rest.marketingcloudapis.com';
var payload = 
    client_id: "xxxx",
    client_secret: "yyyy",
    grant_type: "client_credentials",
    scope: "list_and_subscribers_write"

var url = authEndpoint + '/v2/token';  
var contentType = 'application/json';

try {
   var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
   if(accessTokenRequest.StatusCode == 200) {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.access_token;
} catch (error) {

var deKey = 'C357F4A6-8C96-4812-B659-87A3F2F7E827';
var contentType = 'application/json';
var restUrl = tokenResponse.rest_instance_url;
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];

payload =  '{';
payload += '   "deleteOperationType": "ContactAndAttributes",';
payload += '   "targetList": { ';
payload += '      "listType": { ';
payload += '         "listTypeID":3';
payload += '      },';
payload += '   "listKey": "' + deKey + '"';
payload += '   },';
payload += '   "deleteListWhenCompleted":false,';
payload += '   "deleteListContentsWhenCompleted":true';
payload += '}';

var endpoint = restUrl + 'contacts/v1/contacts/actions/delete?type=listReference';

var result = HTTP.Post(endpoint, contentType, payload, headerNames, headerValues)

1 Answer 1


You are most probably using a newly created credential package together with legacy endpoints. The auth endpoint needs to be updated to v2, as described here. You will also see a difference in how token is provided in the response. Hence the parsing of this should be updated.

You should update this script to use tenant specific endpoint, replacing www.exacttargetapis.com with [yoursubdomain]. rest.marketingcloudapis.com as per this article.

Your snippet looks pretty much as my old code from Trying to automate contact deletion with SSJS - and yes, I know it was deprecated. I have now updated the code to correspond to how the integration endpoints are implemented today.

  • Thank you for your response, I've updated my code based on your articles and it still isn't working. I've edited my post with the new code. Could you have a look? Commented Mar 14, 2020 at 9:31
  • There are multiple issues with your code (e.g. you don't use the correct url for AUTH endpoint). I have updated the code in my old answer accordingly. Please use it for reference. Commented Mar 14, 2020 at 9:50
  • Thank you for updating your answer! I copied your code and changed the personal variables, it still isn't working :/. Commented Mar 14, 2020 at 14:46
  • @user3485470 - you need to perform this operation in root BU. also - did you even enable contact deletion as described here? Commented Mar 14, 2020 at 14:48
  • Yeah I've been doing it on the root BU and yes I have it enabled. I'll just log a case. Commented Mar 15, 2020 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .