Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save katydorjee/1e9f27b2e1b3c9f32dfa05d79cd0dc6b to your computer and use it in GitHub Desktop.

Select an option

Save katydorjee/1e9f27b2e1b3c9f32dfa05d79cd0dc6b to your computer and use it in GitHub Desktop.
API to delete subscriber in Server Side Javascript
<script runat="server">
Platform.Load('Core','1');
var username = 'Username';
var password = 'PaSsWoRd';
var serviceURL = 'https://webservice.s7.exacttarget.com/Service.asmx';
//DE contains all the subscribers to be deleted. Delete all subscribers where field value of IsDeletedFromAllsubscribers is False
var srcDE = DataExtension.Init("Data Extension External key");
var ds = srcDE.Rows.Retrieve({Property:"IsDeletedFromAllsubscribers",SimpleOperator:"equals",Value:"False"});
if(ds.length>0)
{
for (var i = 0; i < ds.length; i++)
{
skey = ds[i].SubscriberKey;
email = ds[i].EmailAddress;
//Constraction of API Envelope
var setCustomerRequest ='<?xml version="1.0" encoding="UTF-8"?>'+
'<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">'+
'<s:Header>'+
'<a:Action s:mustUnderstand="1">Delete</a:Action>'+
'<a:MessageID>urn:uuid:7e0cca04-57bd-4481-864c-6ea8039d2ea0</a:MessageID>'+
'<a:ReplyTo>'+
'<a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>'+
'</a:ReplyTo>'+
'<a:To s:mustUnderstand="1">https://webservice.s7.exacttarget.com/Service.asmx</a:To>'+
'<o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1">'+
'<o:UsernameToken>'+
'<o:Username>'+username+'</o:Username>'+
'<o:Password>'+password+'</o:Password>'+
'</o:UsernameToken>'+
'</o:Security>'+
'</s:Header>'+
'<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
'<DeleteRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">'+
'<Objects xsi:type="Subscriber">'+
'<PartnerKey xsi:nil="true"/>'+
'<ObjectID xsi:nil="true"/>'+
'<EmailAddress>'+email+'</EmailAddress>'+
'<SubscriberKey>'+skey+'</SubscriberKey>'+
'</Objects>'+
'</DeleteRequest>'+
'</s:Body>'+
'</s:Envelope>';
//API service to delete the subscriber
var setCustomerResponse = HTTP.Post(serviceURL, 'text/xml', setCustomerRequest, [], []);
setCustomerResponseStr = Stringify(setCustomerResponse.Response[0]);
setCustomerResponseStr = setCustomerResponseStr.substring(setCustomerResponseStr.indexOf('<StatusMessage>')+'<StatusMessage>'.length);
setCustomerResponseStr = setCustomerResponseStr.substring(0, setCustomerResponseStr.indexOf('</StatusMessage>'));
setCustomerResponseStr = setCustomerResponseStr.replace(/\\/g, '');
//Setting a flag (IsDeletedFromAllsubscribers) in DE confirming that the subscriber is deleted
srcDE.Rows.Update({IsDeletedFromAllsubscribers:"True",DateDeleted:Now(),APIResponse:setCustomerResponseStr}, ["SubscriberKey"], [skey]);
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment