Skip to main content

Delete

Delete One#

Find a Model from the database, then convert it into ActiveModel, finally delete the corresponding row from database.

let orange: Option<fruit::Model> = Fruit::find_by_id(30).one(db).await?;let orange: fruit::ActiveModel = orange.unwrap().into();
let res: DeleteResult = orange.delete(db).await?;assert_eq!(res.rows_affected, 1);

Delete Many#

You can also delete multiple rows from the database without finding each Model with SeaORM select.

// DELETE FROM `fruit` WHERE `fruit`.`name` LIKE '%Orange%'let res: DeleteResult = fruit::Entity::delete_many()    .filter(fruit::Column::Name.contains("Orange"))    .exec(db)    .await?;
assert_eq!(res.rows_affected, 2);