Question
With the GDPR laws in place, it is important to ensure that contacts can be removed from Marketing Cloud. I am currently trying to automate the process of contact deletion using existing tools and looking for possible solutions.
Currently, I am using the Remove()
function from the Subscriber Object in the Core SSJS library to remove contacts. Here is the code I am using:
<script runat="server">
Platform.Load("Core","1");
var subObj = Subscriber.Init("000-T-01-API-Delete");
var status = subObj.Remove();
</script>
This script works without any errors, but the contact remains in the system, even though it was created manually in Mobile Connect and exists in All Contacts. Why is the contact not being deleted?
Is there a better approach for bulk deletion rather than deleting contacts one by one in a loop? I am looking for a more efficient solution that can handle bulk deletions, similar to what the API allows—deleting contacts stored in a data extension.
Answer:
There are several points to address when automating contact deletion in Salesforce Marketing Cloud, particularly with GDPR compliance.
- Issue with the
Remove()
Function: TheRemove()
function from the Core SSJS library works for individual contacts but does not perform a complete deletion from All Contacts. It may not function as expected for contacts created manually or stored in certain areas like Mobile Connect. The issue may also stem from the fact that this function removes the subscriber from the specified list but doesn’t remove the contact from all Marketing Cloud systems. - Solution Using REST API: A more efficient way to automate contact deletion, especially for bulk deletions, is to use the REST API with the
DeleteByListReference
endpoint. This allows you to delete contacts from a data extension, and it works with sendable data extensions. Below is an example of how you can authenticate and call the API to delete contacts:
<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>
This script authenticates using OAuth2, then sends a POST
request to delete contacts based on a reference to a list in a data extension. The key parameters in this request include the deleteOperationType
, which specifies whether to delete the contact and its attributes, and the target list, which should be a sendable data extension.
- Key Considerations:
- Ensure that Contact Deletion is enabled in your Marketing Cloud account. This is a necessary step to be able to delete contacts through the API.
- The Data Extension used for deletion must be marked as sendable, and it should contain at least the
Subscriber Key
column. - When using GDPR functionality, be very cautious as deleting contacts may break SDK communication with Marketing Cloud servers. Ensure you do not delete valid contacts that are still required for communication.
Using the REST API for bulk deletions is more efficient than manually deleting contacts one by one. It can handle large numbers of contacts and provides greater flexibility, allowing you to manage GDPR-compliant contact deletions at scale.
Summing Up
To automate contact deletion in Salesforce Marketing Cloud with GDPR compliance, the Remove()
function from the Core SSJS library is not sufficient as it doesn’t fully delete contacts from All Contacts. A more effective solution is to use the REST API with the DeleteByListReference
endpoint. This allows for bulk deletion of contacts from a sendable data extension, which can be triggered using an SSJS script.
The process involves:
- Authenticating with the Marketing Cloud API using OAuth2.
- Sending a
POST
request to theDeleteByListReference
endpoint with the necessary parameters, such as the data extension key and the deletion operation type. - Ensuring that the Data Extension is marked as sendable and contains the
Subscriber Key
column.
Additionally, Contact Deletion must be enabled in your Marketing Cloud account, and caution is advised to avoid deleting valid contacts that may impact SDK communications. Using the REST API is more efficient for bulk deletions than individual contact removal through a loop.
Kick Start Your Journey with Salesforce Learning
Our Salesforce Course is tailored to give you a deep, hands-on understanding of the Salesforce platform, empowering you to excel in the dynamic world of CRM solutions. We cover all essential modules, including Salesforce Admin, Developer, and AI, with a focus on practical learning. Through real-time project work and scenario-based assignments, you’ll develop the skills to handle real-world business challenges efficiently. Led by industry experts, our program ensures you gain both the knowledge and the confidence needed to thrive in the Salesforce ecosystem.
Along with technical training, our Salesforce training in Bangalore offers personalized mentorship, certification guidance, and interview preparation to help you stand out in the competitive job market. You’ll benefit from comprehensive study materials, live project experiences, and one-on-one support to enhance your expertise. We’re committed to ensuring that by the end of the course, you’ll be fully equipped to tackle Salesforce roles with practical, in-demand skills that employers are actively seeking. Start your Salesforce journey with us and unlock new career opportunities today!