0

We have multiple Data extension which are no longer required for use. I want to delete all the data extension inside a folder in SFMC. is there any way to achieve this using automation or SSJS? I don't want to delete manually as we have many data extensions and it is time consuming.

Thanks,

1

2 Answers 2

2

There is no function inside of SSJS that will let you do this. The Core functions do not have a 'Delete' option for DataExtensions, but you CAN do this via WSProxy.

  1. You would first retrieve each of the DE Object IDs via a retrieve request.
  2. You then would use deleteBatch() to remove each of these data extensions.

In order to run this though, you would need to place the script inside of a 'Script Activity' in Automation Studio and then schedule the automation to run at your set intervals.

Here is a sample function for retrieving the DE objectIds via WSProxy:

function retrieveDEsbyFolder(folder) {
  var cols = ["ObjectID"];
  var filter = {
      "Property": "CategoryID",
      "SimpleOperator": "equals",
      "Value": folder
  }
  var res = prox.retrieve("DataExtension", cols, filter);

  return res;
} 

and here is the function to batch delete the DEs:

function deleteDEsbyFolder(folder) {
  var ret = retrieveDEsbyFolder(folder);
  var deArr = [];
  var results = ret.Results

  for(i=0;i<results.length;i++) {
    var deObj = {};
    var objectID = results[i].ObjectID;
    deObj.ObjectID = objectID;
    deArr.push(deObj);
  }

  var res = prox.deleteBatch("DataExtension",deArr) 
  return res;

}

And here is a sample with everything pushed together:

<script runat="server">

var folder = 12345;
var prox = new Script.Util.WSProxy();

var deDelete = deleteDEsbyFolder(folder);


function deleteDEsbyFolder(folder) {
  var ret = retrieveDEsbyFolder(folder);
  var deArr = [];
  var results = ret.Results

  for(i=0;i<results.length;i++) {
    var deObj = {};
    var objectID = results[i].ObjectID;
    deObj.ObjectID = objectID;
    deArr.push(deObj);
  }

  var res = prox.deleteBatch("DataExtension",deArr) 
  return res;

}

function retrieveDEsbyFolder(folder) {
  var cols = ["ObjectID"];
  var filter = {
      "Property": "CategoryID",
      "SimpleOperator": "equals",
      "Value": folder
  }
  var res = prox.retrieve("DataExtension", cols, filter);

  return res;
} 
</script>

as a note, if you have more than 2500 records, you will need to use Pagination for WSProxy to retrieve these as max return from a retrieve is 2500 records.

4
  • 1
    You beat me to it :D You might consider removing the Platform.Load("Core","1.1.1"); line from the code, though, to speed it up. I don't think you've called any of the Core functions. Jul 7, 2020 at 18:42
  • 1
    Yeah sorry @SamWhitmore, that was an artifact from my debugging it with Write. I will remove shortly. Thanks! Jul 7, 2020 at 19:34
  • I usually put these functions at the top of my code to easily have access to Write & Stringify for debugging: function Write(str) { Platform.Response.Write(str);} function Stringify(obj) { return Platform.Function.Stringify(obj);} Jul 7, 2020 at 20:18
  • Thanks Gortonington . This solution worked for me. I do have query , can this also work for deleting unused emails? Cheers buddy..thanks a ton for the help.
    – RKNauhwar
    Jul 18, 2020 at 4:41
-1

Unfortunately, there is not a way to use SSJS to automate deletion as requested, there is an alternative way to accomplish this so that that desired specific data gets deleted from All Contacts and such, as well as do it in a manner where you do not have to individually delete each one.

If you create a sendable Data Extension with only a Contact Key field, then import the data that needs to be deleted (only keeping the Contact Key), you can delete just the intended data by doing the following:

(from the Parent BU)

  • Select the Contact Builder app (Audience Builder > Contact Builder)
  • Select the All Contacts tab at the top
  • Select All Contacts from the left sidebar
  • Select the delete dropdown (trashcan symbol)
  • Select Delete contacts from the data extension

From there, you can initiate the deletion process. Note that you can choose that same delete dropdown to View Pending Deletions to check the status of that deletion request.

Please note that this will remove all contacts that match the Contact Key listed on the created DE across all provisioned channels, so please make sure that only contacts intended to be removed in that manner are in the Data Extension.

Also please note the following, from the earlier attached documentation concerning Contact Deletion in Contact Builder:

This functionality includes a 14-day default period where Contact Builder suppresses contact information from showing in channel apps such as Email or MobileConnect. You can change the length of this time period in your account. The information remains in your account, but you cannot view contact records or access it to send activities. Once the period elapses, we delete contact information for deleted contacts from lists and sendable data extensions. This process does not apply to any non-sendable data extensions not included in a population via Data Designer, and you are responsible for deleting applicable information from those sources on your own.

  1. Deletion process initiated
  2. Suppression period - contact information remains in your account, but you cannot view or access it
  3. Final deletion - Marketing Cloud removes deleted contact information from your account

I do recommend you to follow best practices to delete contacts: https://help.salesforce.com/articleView?id=mc_cab_best_practices.htm&type=5

This can be achieved as well by REST API call: https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/ContactsDeleteConfigStatus.htm

As additional information, "All Contacts" refer to the contact within Mobile Connect. "All Subscribers" are subscribers within the All Subscribers list in Email Studio. The count for "All Contacts" in mobile connect does not count toward the "All Subscribers" list. However, if you send an email to a contact in "All Contact", that contact will be added to the "All Subscribers" list and therefore adding to the All Subscriber list count.

To have automated deletion for "All Contacts" considered for future deployment, you could go to the Idea Exchange page, and post an idea to have the ability to the custom setting within the Managed package. Our product team monitors these ideas, and the more votes they get, the higher priority they take for future upgrades on releases.

If you would like to do this, you can go to the Idea Exchange by following this link: https://success.salesforce.com/ideaSearch

Thanks.

2
  • All great information, but I think the OP just wanted to delete the DE itself, not the records in the DE. Jul 7, 2020 at 13:06
  • Thanks @gortonington I will take the point. Jul 7, 2020 at 13:09

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