Skip to content

Instantly share code, notes, and snippets.

@wvpv
Created March 21, 2016 14:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvpv/a8e565c3cb27cbd321f7 to your computer and use it in GitHub Desktop.
Save wvpv/a8e565c3cb27cbd321f7 to your computer and use it in GitHub Desktop.
SFMC SSJS Delete Rows from a Data Extension
<script runat="server">
Platform.Load("core", "1.1.1");
function pruneRows () {
var DERowKeys = DataExtension.Init("DEofRowKeys");
var DERowKeyRows = DERowKeys.Rows.Lookup(["ProcessedFlag"], [0], 50, "RowDate");
var returnString = "";
var totalDelCount = 0;
for (var i in DERowKeyRows) {
var RowKey = DERowKeyRows[i].RowKey;
var RowDate = DERowKeyRows[i].RowDate;
returnString += "<br>" + RowKey;
returnString += ", " + RowDate;
var delCount = 0;
var action = "";
var begin = (new Date()).getTime();
var sl = DataExtension.Init("DataExtensionToPrune");
try {
delCount += sl.Rows.Remove(["RowKey"], [RowKey]);
totalDelCount += delCount;
action = "delete";
returnString += ", " + delCount;
} catch (e) {
action = "error";
}
var end = (new Date()).getTime();
var duration = (end-begin).toString() + 'ms ';
// update row as processed
if (action != "error") {
var updateCount = DERowKeys.Rows.Update({"ProcessedFlag":1,"RowsDeleted":delCount, "Duration":duration, "Action":action}, ["RowKey"], [RowKey]);
}
}
returnString += "<br>total deleted in batch: " + totalDelCount;
return returnString;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment