# Delete all records

**URL:** <https://forum.servoy.com/t/delete-all-records/2248>\
**Category:** Classic Servoy\
**Created:** [July 14, 2004, 5:15pm UTC](https://forum.servoy.com/t/delete-all-records/2248 "2004-07-14T17:15:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wgknowles](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wgknowles](https://forum.servoy.com/u/wgknowles)\
**Post date:** [July 14, 2004, 5:15pm UTC](https://forum.servoy.com/t/delete-all-records/2248/1 "2004-07-14T17:15:09Z")

</div>

What’s the quickest way to delete a table’s entire contents?

I have a simple loop:

controller.loadAllRecords()  
var maxi = controller.getMaxRecordIndex()  
for (i=1; i \<= maxi; i++)  
{  
controller.deleteRecord()  
controller.saveData()  
}

This loop doesn’t seem to work every time though.. not sure why!

I tried deleteAllRecords(), but that seems to only affect the foundset, not the actual records in the DB. Maybe I am using it wrong?

---

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [July 14, 2004, 5:22pm UTC](https://forum.servoy.com/t/delete-all-records/2248/2 "2004-07-14T17:22:22Z")

</div>

THIS SHOULD DO THE TRICK:

```auto
controller.loadAllRecords() 
controller.deleteAllRecords()

```

or this: (much slower! not preferable!)

```auto
controller.loadAllRecords() 
for (i=1; i <= controller.getMaxRecordIndex(); i++) 
{ 
controller.deleteRecord() 
}
controller.saveData()

```

you must set the controller.getMaxRecordIndex() inside the loop and not outside. Than it goes behond the limit of every 200 records![/code]

---

<div class="post-metadata">

**Author:** ![wgknowles](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wgknowles](https://forum.servoy.com/u/wgknowles)\
**Post date:** [July 14, 2004, 5:35pm UTC](https://forum.servoy.com/t/delete-all-records/2248/3 "2004-07-14T17:35:57Z")

</div>

I saw some wierd behavior when putting get max records inside the loop, Every iteration that number changes and you end up only deleting half the records, when i meets the max record index.

I tried the first suggestion It works now… wierd

I added a controller.saveData() to be safe.

---

<div class="post-metadata">

**Author:** ![Odysseus](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@Odysseus](https://forum.servoy.com/u/Odysseus)\
**Post date:** [July 15, 2004, 10:59am UTC](https://forum.servoy.com/t/delete-all-records/2248/4 "2004-07-15T10:59:57Z")

</div>

I maybe misunderstanding things, but personally ,when working with sybase at the backend, it seems to me that an executesql command that says “truncate table xxxx” would delete all records of the table, without the overhead of every delete added to the log (as stated in the sybase docs) and eliminating the need for a potentially heavy loop

HTH

---

<div class="post-metadata">

**Author:** ![wgknowles](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wgknowles](https://forum.servoy.com/u/wgknowles)\
**Post date:** [July 15, 2004, 3:13pm UTC](https://forum.servoy.com/t/delete-all-records/2248/5 "2004-07-15T15:13:18Z")

</div>

You can’t execute such a command directly from within Servoy.

I imagine that deleteAllRecords() does something to that effect for you.

---

<div class="post-metadata">

**Author:** ![Odysseus](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@Odysseus](https://forum.servoy.com/u/Odysseus)\
**Post date:** [July 15, 2004, 4:04pm UTC](https://forum.servoy.com/t/delete-all-records/2248/6 "2004-07-15T16:04:10Z")

</div>

This procedure (using Sybase central to build) in your database (obviously changing the table name to the correct one)

ALTER PROCEDURE “DBA”.“deleteAllClients”( /\* [IN | OUT | INOUT] parameter\_name parameter\_type [DEFAULT default\_value], … _/ )  
/_ RESULT( column\_name column\_type, … ) \*/  
BEGIN  
truncate table dummy  
END

can be called and functions from servoy with this very basic code

var array1 = new Array();  
var array2 = new Array();

databaseManager.executeStoredProcedure(‘ready2learn’, “call deleteAllClients()”,array1,array2,0);

I have to say that on my dummy table it took surprisingly long (for 3 records , a few seconds) on the other hand I believe that for truncate it does really matter if its 3 or 500000 records; anyway this is a way for executing sql on your db backend. The executesql statement came from another language ![:roll:]( "Rolling Eyes")

Of course both the procedure and the method can be enhanced i.e. passing the table name as a variable,…

HTH

---

<div class="post-metadata">

**Author:** ![wgknowles](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wgknowles](https://forum.servoy.com/u/wgknowles)\
**Post date:** [July 15, 2004, 4:19pm UTC](https://forum.servoy.com/t/delete-all-records/2248/7 "2004-07-15T16:19:06Z")

</div>

I may be mistaken about the functionality of databaseManager.executeStoredProcedure(), but in my understanding:

You should be careful using stored procedures to modify records. Servoy has gone to great lengths to ensure data consistency across simultaneous connected clients, and changing anything on the database level without servoy knowing about it can result in errors corrupted datasets and other undesired behavior.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [July 23, 2004, 9:42am UTC](https://forum.servoy.com/t/delete-all-records/2248/8 "2004-07-23T09:42:19Z")

</div>

this code:

```auto
controller.loadAllRecords() 
controller.deleteAllRecords()

```

should work fine. Doesn’t it delete all records in youre case??

---

<div class="post-metadata">

**Author:** ![wgknowles](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wgknowles](https://forum.servoy.com/u/wgknowles)\
**Post date:** [August 5, 2004, 5:13pm UTC](https://forum.servoy.com/t/delete-all-records/2248/9 "2004-08-05T17:13:51Z")

</div>

I must have been doing something wierd at first, it works fine now.
