🔥 600+ people already subscribed. Why not you? Get our newsletter with handy code snippets, tips, and marketing automation insights.

How to delete data extensions using SSJS in Salesforce Marketing Cloud Engagement

Highlight text, then choose “Suggest an edit”.Your selection is saved in the bottom bar.

Managing Data Extensions in Salesforce Marketing Cloud can quickly become messy – especially when test data or outdated structures pile up. Unfortunately, there’s no built-in way to bulk-delete DEs through the UI – at least not from various folders. The good news? You can do it programmatically using SSJS and WSProxy.

In this guide, I’ll walk you through a safe and scalable approach to deleting Data Extensions with just a few lines of code, plus highlight some gotchas to avoid when working with relationships or retention policies.

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

    var deCustomerKeys = [
        'customer_key_to_delete'
    ],
    i = 0,
    result,
    api = new Script.Util.WSProxy();
  
  try{    
        for (i=0;i<deCustomerKeys.length;i++){
            
            result = api.deleteItem("DataExtension", { 
                "CustomerKey": deCustomerKeys[i]
            });
            if (result.Status == "Ok")
              Write("Deleted - " + deCustomerKeys[i]);
            else
              Write(Stringify(result))
        }
    }catch(e){
        Write(Stringify(e))
    }
</script>

Another approach to deleting Data Extensions with SSJS is to first save all the target CustomerKey values into a separate Data Extension, along with an isProcessed flag. This allows you to resume the deletion process seamlessly across multiple script executions, even if the 30-minute run-time limit is exceeded.

You can also parse the response to log or save anything you need.

{
    "Status": "OK",
    "RequestID": "ead10751-5bd2-4357-b007-60e018020d3a",
    "Results": [
        {
            "Object": {
                "CustomerKey": "test_delete",
                "Name": null,
                "Description": null,
                "IsSendable": false,
                "IsTestable": false,
                "SendableDataExtensionField": null,
                "SendableSubscriberField": null,
                "Template": null,
                "DataRetentionPeriodLength": 0,
                "DataRetentionPeriodUnitOfMeasure": 0,
                "RowBasedRetention": false,
                "ResetRetentionPeriodOnImport": false,
                "DeleteAtEndOfRetentionPeriod": false,
                "RetainUntil": null,
                "Fields": null,
                "DataRetentionPeriod": "Days",
                "CategoryID": 0,
                "Status": null,
                "Client": null,
                "PartnerKey": null,
                "PartnerProperties": null,
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "ObjectID": "54a0b136-4b61-f011-a5bb-5cba2c7b42b0",
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "StatusCode": "OK",
            "StatusMessage": "Data Extension deleted.",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

Sometimes, we don’t have the CustomerKey of a Data Extension readily available and would prefer to delete it by name. Unfortunately, even though the n…

🔒 Premium Subscriber Content

What you can learn with Premium

Unlock the full article to explore these topics in detail.

  • Data Extensions Linked to Attribute Groups Cannot Be Deleted
  • Data Extensions Deleted via SSJS Can Be Restored
  • System Performance Degrades with Too Many Data Extensions

Already a Premium subscriber? Log in to continue reading, or register to create your account.

You must log in and have a Premium Subscriber account to read the full article.

When upgrading, please use the same email address as your WordPress account so we can correctly link your Premium membership.

Please allow us a little time to process and upgrade your account after the purchase. If you need faster access or encounter any issues, feel free to contact us at info@martechnotes.com or through any other available channel.

To join the Discord community, please also provide your Discord username after subscribing, or reach out to us directly for access.

You can subscribe even before creating a WordPress account — your subscription will be linked to the email address used during checkout.

Premium Subscriber

29,99 € / Year

  • Free e-book with all revisions - 101 Adobe Campaign Classic (SFMC 101 in progress)
  • All Premium Subscriber Benefits - Exclusive blog content, weekly insights, and Discord community access
  • Lock in Your Price for a Full Year - Avoid future price increases
  • Limited Seats at This Price - Lock in early before it goes up
  • Monthly 15-Minute Call - Discuss any topic directly

Oh hi there đź‘‹
I have a SSJS skill for you.

Sign up now to get an SSJS skill that can be used with your AI companion

We don’t spam! Read our privacy policy for more info.

Share With Others

The Author
Marcel Szimonisz Platinum

Marcel Szimonisz

Marketing Automation Consultant

I specialize in solving marketing technology challenges, automating processes, and turning ideas into practical solutions with Salesforce Marketing Cloud and Adobe Campaign. Through MarTechNotes, I share technical guides and insights that make complex topics easier to understand

Your email address will not be published. Required fields are marked *

Get exclusive tips, scripts and news

Choose your topics

We don’t spam! Read our privacy policy for more info.

Similar posts
Add to this article

Share your experience

Your contribution

Article history

What changed