🔍 You can now filter content to find what interests you! Log in to use the filters. New here? Register and finish setting up your account to get started.

background shape
background shape

How to Clear a Data Extension Using SSJS in SFMCE

Sometimes you need to remove every record from a Data Extension before importing or generating a fresh dataset. In Salesforce Marketing Cloud, you can do this with SSJS and the WSProxy ClearData action.

Unlike deleting individual rows, this operation clears the entire Data Extension in a single request. The Data Extension itself – including its fields and configuration – is preserved.

Warning: The operation permanently deletes all records from the Data Extension. It cannot be filtered or undone.

Basic SSJS example

Create a Script Activity in Automation Studio and use the following code:

<script runat="server">
    Platform.Load("Core", "1");
    var prox = new Script.Util.WSProxy();
    var properties = {CustomerKey: "YOUR-DATA-EXTENSION-EXTERNAL-KEY" };
    var result = prox.performItem( "DataExtension", properties, "ClearData", { } );
</script>

Reusable function

If your script clears multiple Data Extensions, you can move the logic into a function:

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

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

    function clearDataExtension(customerKey) {
        var properties = { CustomerKey: customerKey };
        return prox.performItem("DataExtension", properties, "ClearData", {});
    }
    try {
        var result = clearDataExtension("YOUR-DATA-EXTENSION-EXTERNAL-KEY");
        if (result.Status !== "OK" || !result.Results || result.Results[0].StatusCode !== "OK") {
            throw new Error("The Data Extension could not be cleared: " + Stringify(result));
        } 
        Write("Data Extension cleared successfully.");
    } catch (error) {
        Write("Error: " + Stringify(error));
    } 
</script>

Checking both the main Status and the result-level StatusCode prevents the script from treating an incomplete or failed request as successful.

Things to remember

  • Use the Data Extension’s external key (<strong>CustomerKey</strong>), not its name.
  • The operation deletes every row and does not support filters.
  • The Data Extension structure and configuration are not deleted.
  • Place the clearing activity before the activity that loads the replacement data.
  • Avoid clearing a Data Extension while it is being used for sending, queries, imports, or Journey Builder entry.

For deleting only selected records, use a row-deletion method with a primary-key value instead of ClearData.

Reference: Clear all Data Extension records using SSJS

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

MarTech consultant

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms, particularly Salesforce Marketing Cloud and Adobe Campaign.

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

Subscribe

Get exclusive tips, scripts and news

Choose your topics

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

Similar posts
[mautic type='focus' id='1']