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







