Delete all database values

export function cleanButtonOnClick(event, $w) {
$w(“#dynamicDataset”).onReady( () => {
$w(“#dynamicDataset”).remove();
while ($w(“#dynamicDataset”).hasNext()){
$w(“#dynamicDataset”).next();
$w(“#dynamicDataset”).remove();
}
});
$w(“#dynamicDataset”).refresh()
}

I have this code to clean up my database with a button click, although it seems like it doesn’t change it at all…

Why doesn’t it work?

Thanks!

Well the logic is right, as long as there is another item keep deleting…Not sure about the syntax though.
What error do you get on the developer console?

1 Like

Don’t get an error at all…

Anyone?

Not on the editor’s console, on the browser console

No :

Hi,
Note that the remove function returns a Promise . Therefore, you should use Async Await :

export function cleanButtonOnClick(event, $w) {
	$w("#dynamicDataset").onReady(async() => {
		if ($w("#dynamicDataset").getCurrentItem()) {
			await $w("#dynamicDataset").remove();
			while ($w("#dynamicDataset").hasNext()) {
				await $w("#dynamicDataset").next();
				await $w("#dynamicDataset").remove();
			}
		}
	});
	$w("#dynamicDataset").refresh();
}

Good luck :slight_smile:
Tal.

Hi kavila31415

Have you perhaps gotten this code to work? I tried it on my end, but after deleting 2 rows, it stops…

Thank you
Tiaan

Hi Tiaan,

Use this version:

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();
}
2 Likes

Thank you Ido!

Hi ido,
Above cleaning code is not working anymore…
Can you please advise ?

Regards,
py

this might work

function clear() {
	//let user = wixUsers.currentUser; 
	//let userId = user.id; 
	// const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
	wixData.query("yourdatabase") // get the item from the database collection.
	.eq("userId")// if any specific field ("userId", userId)
	.find()
    .then((results) => {
	  removeItems(results.items);
	    console.log(removeItems);
});
}
async function removeItems(items) {
    items.forEach(async (item, i) => {
        await wixData.remove("yourdatabase", items[i]._id);
        console.log(removeItems);
    });
}