Collection import replace

I have a collection that I will be importing data from a CSV file. This CSV file is generated from a local database application and is updated on the local system 2 times a month, which we then generate a new CSV file.

The CSV file (approximately 900 rows, but the total changes each time) imports as expected into the Collection. But, the imported rows are added to the current Collection. And if we sync the data to the live Collection, the data is added to that Collection as well.

So, for example, if we have a sandbox collection of 900 rows, importing a new CSV file results in 1800 rows and so forth and so on.

We need the imported CSV file to completely replace the current database contents during import and when syncing to the live collection (even if the imported CSV file has fewer rows). Or as an option, at least be able to easily completely empty a collection before importing a new CSV file.

Thanks for any suggestions offered.

1 Like

Hi Jim,

Welcome to the Wix Code forum.

You can use one of these two methods to delete the contents of a collection:

export function button8_click(event, $w) {
   wixData.query("NewCollectionName”)
   .limit(1000)
   .find()
   .then((result) => {
      for (var i = 0; i < result.items.length; i++){
         if (result.items[i] === null) continue;
         wixData.remove("NewCollectionName", result.items[i]._id);
      }
      console.log('erase done');
   });
}
export function cleanButtonOnClick(event, $w) {
   let dataset = $w("#dynamicDataset"); // or the name of your dataset
   dataset.onReady(async() => {
      while (dataset.getCurrentItem()) {
         await dataset.remove();
      }
   });
   dataset.refresh();
}

Just call whichever function you want from an admin page where you can click a button to call the routine and empty your collection.

I hope this helps,

Yisrael

Hi Yisrael and thanks for the info. It appears that the 1st technique works the best. I did need to add the following for it work.

import wixData from 'wix-data'; 

But, the time it takes is kind of all over the place. On one run it took about 5 minutes, and on another it took around 30 seconds (I had re-imported the data to test it). And in the sandbox preview the “erase done” prompt popped up in the page code window within a couple of seconds, the actual deletion was still ongoing.

Is there any method of displaying a progress indicator? Like “100 of 900 removed” or “11% complete”? The person who will be responsible for doing this every other week, will need some “hand holding” as it were.

Thanks again.

Hi Yisrael, I used the first option but keep getting an error on the code. I have tried moving ) or }. Also, I tried changing names of collection. I am sure it more basic error than that. Can you help please.

is there a way to do this not programatically - it’s a pretty basic database operation… and the ways suggested seems like a very “bad practice”