# Deleting multiple records in a foundset

**URL:** <https://forum.servoy.com/t/deleting-multiple-records-in-a-foundset/20808>\
**Category:** Questions\
**Created:** [July 14, 2022, 12:57am UTC](https://forum.servoy.com/t/deleting-multiple-records-in-a-foundset/20808 "2022-07-14T00:57:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![roddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@roddy](https://forum.servoy.com/u/roddy)\
**Post date:** [July 14, 2022, 12:57am UTC](https://forum.servoy.com/t/deleting-multiple-records-in-a-foundset/20808/1 "2022-07-14T00:57:25Z")

</div>

When deleting records from a foundset, I have used the following approaches but I am still getting occasional “Update/insert failed, unexpected nr of records affected: expected 1, actual 0” exceptions. I have tried two different methods for achieving this, shown below; is there a preferred way of deleting multiple records from a foundset to avoid the exception being thrown?

```auto
function clearEmptyItems(recInvoice)
{
	var numItems = 0;
	var recItems = [];

	if (utils.hasRecords(recInvoice.invoices_to_invoice_items))
	{
		numItems = recInvoice.invoices_to_invoice_items.getSize();
	}

	// As we will be deleting the records from the foundset, gather each of them so we don't rely on the (dynamic) index position
	for (var index = 1; index <= numItems; index++)
	{
		var recItem = recInvoice.invoices_to_invoice_items.getRecord(index);
		
		if (isItemEmpty(recItem))
		{
			recItems.push(recItem);
		}
	}
	
	// Delete the item records from the foundset
	recItems.forEach(function(recItemToDelete)
	{
		recInvoice.invoices_to_invoice_items.deleteRecord(recItemToDelete);
	});
}

```

and

```auto
	for (var index = 1; index <= recInvoice.invoices_to_invoice_items.getSize(); index++)
	{
		var recItem = recInvoice.invoices_to_invoice_items.getRecord(index);
		
		if (isItemEmpty(recItem))
		{
			recInvoice.invoices_to_invoice_items.deleteRecord(index);
			index--;
		}
	}

```
