2

with the GDPR laws in place, it's important to have the possibility to remove contacts from Marketing Cloud. I'm trying to automate the process with existing tools and am looking for possible solutions.

Right now I'm trying to use the Remove() function from the Subscriber Object in the Core SSJ library:

<script runat="server">

    Platform.Load("Core","1");

    var subObj = Subscriber.Init("000-T-01-API-Delete");

    var status = subObj.Remove();

</script>

The contact was created manually in Mobile Connect and exists in All Contacts. Although the query completes without errors, the contact remains in the system.

Any idea how to fix the script to perform the deletion?

I am thinking this might not be the best approach since it would be a 1 by 1 deletion of contacts performed in a loop, but I don't see an option like the API allows to tell the system to delete contacts stored in a data extension.

Any advice or ideas how to automate this process would be greatly appreciated.

22

Updated March 2020 to accomodate for v2 authorisation and tenant specific endpoints.

Please see the example of how I use this for bulk deletion. You should use the REST API, using the DeleteByListReference endpoint. Here it works together with a sendable DE, and SSJS activity in Automation Studio:

<script runat="server">
    Platform.Load("Core", "1");
    //AUTHENTICATE
    var url = "https://[yoursubdomain].auth.marketingcloudapis.com/v2/token";
    var contentType = "application/json";
    var payload =
        '{"grant_type": "client_credentials","client_id": "[YOURID]","client_secret": "[YOURSECRET]","account_id":"[YOURMID]"}';

    var accessTokenResult = HTTP.Post(url, contentType, payload);
    var accessToken = Platform.Function.ParseJSON(accessTokenResult["Response"][0]).access_token;

    if (accessToken != "") {
        //EXECUTE
        try {
            var deleteUrl =
                "https://[yoursubdomain].rest.marketingcloudapis.com/contacts/v1/contacts/actions/delete?type=listReference";
            var payload1 =
                '{"deleteOperationType": "ContactAndAttributes","targetList": {"listType": {"listTypeID": 3},"listKey": "[Data Extension External Key]"},"deleteListWhenCompleted": false,"deleteListContentsWhenCompleted": false}';
            var headerNames = ["Authorization"];
            var s1 = "Bearer ";
            var headerValues = [s1.concat(accessToken)];
            var result = HTTP.Post(deleteUrl, contentType, payload1, headerNames, headerValues);
        } catch (ex) {
            Write("Exception Error: " + Stringify(ex));
        }
    }
</script>

Remember to have the Data Extension created as sendable, and the only column required is the Subscriber Key. Also do ensure that Contact Deletion is enabled on your account. Here is how you do it.

Warning: When deleting contacts using the GDPR functionality you will render the SDKs unable to communicate with the Marketing Cloud servers. You need to be very, very careful to ensure that you're not deleting contacts that are still valid.

16
  • 3
    Lukas, I'm extremely grateful for this answer. It's really a great piece of code for someone like me that's just learning to use the API and scripts in Automation Studio. With proper deletion settings (suppression period set to 0) the contact is already gone from the system :) Thank you so much! Jan 8 '19 at 13:46
  • 5
    Please find here a more detailed code with error handling by @adamspriggs gist.github.com/wvpv/…
    – zeljazouli
    Jan 8 '19 at 14:54
  • 1
    Yes, @garek007 - you need to run it in root BU. Also, it might take up to days or even weeks, depending on the volume of contacts and data extensions in your account Oct 19 '20 at 21:19
  • 1
    Yes, @MarcelGentSimonis - once the SSJS has been executed and deletion process has started, you can overwrite the referenced DE with a new set of Contact Keys May 31 at 7:36
  • 1
    @Marcel - that will not be a problem May 31 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.