Hi
I have import my test data (1000 records) into a collection, now I would like to clear all the data both on Sandbox and Live, is there a way that I can clear all the data in a collection?
Thanks
Jack
Hi
I have import my test data (1000 records) into a collection, now I would like to clear all the data both on Sandbox and Live, is there a way that I can clear all the data in a collection?
Thanks
Jack
Hi Jack!
Use this code snippet to delete the items:
function clear() {
wixData.query("yourCollection")
.limit(1000)
.find()
.then((results) => {
removeItems(results.items);
console.log(removeItems);
});
}
async function removeItems(items) {
await items.forEach(async (item, i) => {
$w('#text1').text = String(i);
await wixData.remove("yourCollection", items[i]._id);
});
}
Remember to adjust the field keys/collection names to fit your site.
Note that the query by default returns only the 50 first items, therefor make sure to change the limit of the query to up to 1000.
Hope it helps!
Doron.
it’s absolutely ridiculous this is the only way to remove all data from a collection without deleting the collection.
yes they should have some option in the GUI to erase all the collection contents
seen this being requested in the feature requests section last year
Hi, pretty new to Code.
On which page would you put that code?
Does it matter, can it be any of them?
Also do you then just delete it and go on as normal once it is done?
Thanks for your time in advance, ste.
You can pretty much use that code anywhere. Depending on your use case and the sensitivity of the data, you might want to put this code in a backend file, and invoke it from a password protected page.
Hi,
What’s doing exactly the “$w(‘#text1’).text = String(i);” part. I put the code into a button (just for testing) and it is not working. I had to comment that part since I’m not sure where to point.
Please, advice.
Thanks in advance.
@hjgr This post is being closed as it is quite old.
Also, there is now a new function bulkRemove() which makes this process much easier.
Regarding your question… It seems that line is used to indicate progress. If you had a text field with the ID #text, it would show the current item being removed.