040.060. Delete, Truncate, and Purge

There are several ways to remove instances of data, clear tables, and even purge entire databases with the Sterling engine. The b. Database Backup and Restore can be used to ensure data is not permanently lost. You may also want to read the 7. Sterling Recipes for examples of creating pseudo-transactions.

Deleting Individual Instances

To delete an individual instance, you can pass either an instance or a key to the engine: 

// delete by instance
var actual = _databaseInstance.Load<TestModel>(key);

// delete by key
_databaseInstance.Delete(typeof(TestModel), key);

You can receive notification before an instance is deleted (and even prevent deletion by throwing an exception) by using a trigger. Learn more by reading about g. Triggers and Interceptors.

Truncating the Entire Table

To truncate the entire table, which essentially deletes all instances and flushes both indexes and keys, simply pass the type of the table to the database engine like this: 


Note that truncate bypasses all triggers.

Purge the Entire Database

The Sterling tests must purge the entire database each time to start with a "clean slate." The command to purge a database is simple (and irreversible if you have not generated a backup):


The database will be usable after the purge, but will, of course, be empty until you save a new instance to it.