# Collection import replace

**URL:** <https://forum.wixstudio.com/t/collection-import-replace/41090>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [May 23, 2018, 6:06pm UTC](https://forum.wixstudio.com/t/collection-import-replace/41090 "2018-05-23T18:06:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jim](https://forum.wixstudio.com/u/jim)\
**Post date:** [May 23, 2018, 6:06pm UTC](https://forum.wixstudio.com/t/collection-import-replace/41090/1 "2018-05-23T18:06:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [May 24, 2018, 7:29am UTC](https://forum.wixstudio.com/t/collection-import-replace/41090/2 "2018-05-24T07:29:54Z")

</div>

Hi Jim,

Welcome to the Wix Code forum.

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

```javascript
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');
   });
}

```

```javascript
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

---

<div class="post-metadata">

**Author:** ![jim](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jim](https://forum.wixstudio.com/u/jim)\
**Post date:** [May 24, 2018, 12:56pm UTC](https://forum.wixstudio.com/t/collection-import-replace/41090/3 "2018-05-24T12:56:13Z")

</div>

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.

```javascript
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.

---

<div class="post-metadata">

**Author:** ![calvojose0](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@calvojose0](https://forum.wixstudio.com/u/calvojose0)\
**Post date:** [April 19, 2019, 8:27pm UTC](https://forum.wixstudio.com/t/collection-import-replace/41090/4 "2019-04-19T20:27:27Z")

</div>

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.

![](https://us1.discourse-cdn.com/wix/original/3X/0/b/0b6a0667187fa44663d066ff4f339b8bb821b4c7.png)

---

<div class="post-metadata">

**Author:** ![danerezsd](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/danerezsd/32/14708_2.png) [@danerezsd](https://forum.wixstudio.com/u/danerezsd)\
**Post date:** [September 23, 2019, 11:44am UTC](https://forum.wixstudio.com/t/collection-import-replace/41090/5 "2019-09-23T11:44:56Z")

</div>

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”
