# Repository: Deleting Data
You may delete existing data through various repository methods.
In this section, it assumes you're familiar with the usage of repository. If not, please read through the Repository: Getting Started page first.
# Deleting Data
To delete a record, you may use the destroy
method and pass the primary key for the record to be deleted.
// Existing records.
[
{ id: 1, name: 'John Doe', age: 40 },
{ id: 2, name: 'Jane Doe', age: 30 },
{ id: 3, name: 'Johnny Doe', age: 20 }
]
// Delete the record with id of 2.
store.$repo(User).destroy(2)
// The result.
[
{ id: 1, name: 'John Doe', age: 40 },
{ id: 3, name: 'Johnny Doe', age: 20 }
]
In addition, the destroy
method will accept an array of primary keys to delete multiple records.
store.$repo(User).destroy([1, 2])
The destroy
method will return deleted models. When you pass a single primary key, it will return a single model, and if you pass multiple primary keys, it will return a collection of models.
const user = await store.$repo(User).destroy(2)
// User { id: 2, name: 'Jane Doe', age: 30 }
const user = await store.$repo(User).destroy([1, 2])
/*
[
User { id: 1, name: 'John Doe', age: 40 },
User { id: 2, name: 'Jane Doe', age: 30 }
]
*/
If you wish to delete the entire records, you may use the flush
method.
store.$repo(User).flush()
# Deleting Data By Query
You can also run a delete statement on a set of records. In this example, we will delete all flights that are marked as inactive.
store.$repo(User).where('active', false).delete()
# Deleting data by model instance method
You may delete a specific record with the model $delete
instance method.
const user = store.$repo(User).find(1)
user.$delete()
← Updating Data Plugins →