Skip to content

Deleting a Model

Jeff Hurray edited this page Apr 25, 2016 · 1 revision

Models conforming to SQLiteModel can be deleted both statically and as instances.

####Static All models conforming to SQLiteModel have the static methods delete and deleteAll.

The following is an example of how to delete every Movie in your database.

try Movie.deleteAll()

delete has a Query parameter, so you can delete specific rows.
The following is an example of how to delete movies that made under 1000 dollars of gross.

let query = Movie.query.filter(Movie.Gross < 1000)
try Movie.delete(query)

Query is a type declared in SQLite.swift. More info can be found here

###Instance

All instances of model types conforming to SQLiteModel have the delete method.

Calling delete will remove the row associated with the instance from the table associated with the model, and also clear the cache of any data associated with the instance.

// Probably a good choice to delete :)
let id = indianaJonesAndTheCrystalSkull.localID
try indianaJonesAndTheCrystalSkull.delete()

let shouldBeNil: Movie? = try Movie.find(id) // nil

###Relationships

Relationships will cascade on delete, so its probably a good idea to keep singular relationships as optionals if those values may be deleted.

let aNewHope = empireStrikesBack => Movie.Prequel
try aNewHope.delete()
let shouldBeNil: Movie? = empireStrikesBack => Movie.Prequel // nil
Clone this wiki locally