Loading

Stop All Running Journey Versions and Delete Journeys in Bulk with SSJS

Data pubblicazione: Sep 11, 2023
Descrizione

In the business unit in which you need to stop Journeys and delete them, create a DE with the following specifications:

Name: journeytodelete
External Key: journeytodelete
 

Field NameData TypeLengthPrimary KeyNullableDefault Value
journeynameText254NoNoNot Applicable
isstoppedBooleanNot ApplicableNoNoFalse
isdeletedBooleanNot ApplicableNoNoFalse


After the DE has been created, populate the DE with journeyname for Journeys that need to be stopped and deleted. 

You will need to create two SSJS activities - one to stop the Journeys and another to delete them. These activities will need to be run sequentially - as only Journeys where all versions are currently stopped are eligible for deletion. 


1. Stop all Journey Versions of supplied Journeys: 

NOTE: Please ensure that the clientId, clientSecret, and your account's authEndpoint values are supplied. This process leverages REST API - so you will need to reference an appropriate Installed Package within your account

<script runat="server">
  Platform.Load("Core", "1.1.1");

  var clientId = 'YOUR CLIENT ID HERE';
  var clientSecret = 'YOUR CLIENT SECRET HERE';

  var authEndpoint = 'https://YOUR_SUBDOMAIN.auth.marketingcloudapis.com/v2/token';
  var contentType = 'application/json';
  
  var payload = {
    client_id: clientId,
    client_secret: clientSecret,
    grant_type: 'client_credentials'
  };

    var accessTokenRequest = HTTP.Post(authEndpoint, contentType, Stringify(payload));
    var accessTokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
    var accessToken = accessTokenResponse.access_token;
    var resturl = accessTokenResponse.rest_instance_url 
 var api = new Script.Util.WSProxy();
 var rowstoprocess = DataExtension.Init("journeytodelete").Rows.Lookup(["isstopped"],[0])
    
    for(p=0; p < rowstoprocess.length; p ++ ) 
    {
 
  var journeyName = rowstoprocess[p].journeyname;
 
  var getendpoint = resturl + "interaction/v1/interactions/?nameOrDescription=" + journeyName +"&mostRecentVersionOnly=false";
    var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
  var contentType = 'application/json';
  var payload = '';
var response = HTTP.Get(getendpoint, headerNames, headerValues);

var responseBody = Platform.Function.ParseJSON(response.Content);
var journeyid = responseBody.items[0].id
    for(j=0; j < responseBody.length; j ++ ) 
    {
      var versionnum = responseBody.items[j].version
        var status = responseBody.items[j].status
      var stopendpoint = resturl + "/interaction/v1/interactions/stop/" + journeyid + "?versionNumber="  + responseBody.items[j].version
    
    if (typeof versionnum === 'undefined') {
     
    } else {
if (status == "Unpublished" || status === "Published"){ var response2 = HTTP.Post(stopendpoint, contentType, payload, headerNames, headerValues);
} 
      else {}}
    } 
      DataExtension.Init('journeytodelete').Rows.Update({"isstopped": "1"}, ["journeyname"], [journeyName]);
    }
</script>

 

2. Delete Stopped Journeys 

NOTE: Please ensure that the clientId, clientSecret, and your account's authEndpoint values are supplied
<script runat="server">
  Platform.Load("Core", "1.1.1");

  var clientId = 'YOUR CLIENT ID HERE';
  var clientSecret = 'YOUR CLIENT SECRET HERE';

  var authEndpoint = 'https://YOUR_SUBDOMAIN.auth.marketingcloudapis.com/v2/token';
  var contentType = 'application/json';
  
  var payload = {
    client_id: clientId,
    client_secret: clientSecret,
    grant_type: 'client_credentials'
  };

    var accessTokenRequest = HTTP.Post(authEndpoint, contentType, Stringify(payload));
    var accessTokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
    var accessToken = accessTokenResponse.access_token;
    var resturl = accessTokenResponse.rest_instance_url 

 var api = new Script.Util.WSProxy();
 var rowstoprocess = DataExtension.Init("journeytodelete").Rows.Lookup(["isdeleted"],[0])
    
    for(p=0; p < rowstoprocess.length; p ++ ) 
    {
 
  var journeyName = rowstoprocess[p].journeyname;
  var getendpoint = resturl + "interaction/v1/interactions/?nameOrDescription=" + journeyName;
    var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var response = HTTP.Get(getendpoint, headerNames, headerValues);

var responseBody = Platform.Function.ParseJSON(response.Content);
var journeyid = responseBody.items[0].id

  var deleteendpoint = resturl + "interaction/v1/interactions/" + journeyid;
  var auth = 'Bearer ' + accessToken;
  var req = new Script.Util.HttpRequest(deleteendpoint);
  
  req.setHeader("Authorization", auth);
  req.method = "DELETE"; 

  var resp = req.send();

DataExtension.Init('journeytodelete').Rows.Update({"isdeleted": "1"}, ["journeyname"], [journeyName]);      
    }
</script>
Risoluzione
Run the above SSJS activities sequentially to stop all Journey versions and delete any Journeys within the DE "journeytodelete".

If either SSJS activity errors with a timeout (30mins) run the activity again to continue stopping and deleting Journeys as desired. 
Numero articolo Knowledge

000395782

 
Caricamento
Salesforce Help | Article