How to delete data extensions using SSJS in Salesforce Marketing Cloud
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. Unfortunat
đź”’ This content is for Premium Subsribers only.
Please log in to preview content. Log in or Register
You must log in and have a Premium Subscriber account to preview the content.
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
1.99 € / Month
- Get Access to Exclusive Blog Content - Insights you won't find anywhere else
- Get Fresh Weekly Insights - Only with Premium Access
- Join the Free Discord Community - Connect with your peers
- Ask Unlimited Questions - Get expert help when you need it
- Limited Seats at This Price - Lock in early before it goes up