Prune rows in a Data Extension with SSJS

Say you want to delete a million rows from a Data Extension in Salesforce Marketing Cloud (SFMC). Doing that with the SFMC Web Services can be a pain. One alternative is to utilize Server-Side JavaScript (SSJS) and a Landing Page.

I built a landing page that posts to itself and runs this pruneRows() function. I configured a Data Extension that contains the keys to delete. The script retrieves all of the rows with that key, deletes them and updates the DE of keys with the results.

You may have to experiment with the number of Row Keys to retrieve at a time. If the number is too high, the landing page will time-out. In my scenario, I found that processing 50 at a time worked well (line #8).

For further reading:

Leave a Reply

2 Comments on "Prune rows in a Data Extension with SSJS"

Notify of
Sort by:   newest | oldest | most voted

Hey Adam– love your stuff!

I was playing around implementing this but I’m terrible at javascript so wasn’t able to set this up. I started to look around alternative routes.

I was able to set this up using ampscript, and a nice little bonus — it doesn’t seem to require me to loop through 50 at a time.

code in landingpage:

VAR @timeBefore, @timeAfter, @count
SET @timeBefore = now()
SET @count=DeleteData(“DataExtensionToPrune”,”RowKey”,RowKey)
SET @timeAfter = now()

total deleted in batch: %%=v(@count)=%%

Started: %%=v(@timeBefore)=%%
Ended: %%=v(@timeAfter)=%%