DELETE
This article provides an overview and how-to instructions for deleting documents using the EVICT DQL operation.
Evicting Data
For complete DQL syntax, see Ditto Query Language (DQL) > EVICT
Evicting Multiple Documents in a Collection
The EVICT operation functions based on a condition, allowing updates to multiple documents simultaneously.
For example, the following snippet, once executed, purges all blue cars stored in the local Ditto store.
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"EVICT FROM cars WHERE color = 'blue'",
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);Referencing Previously Evicted Documents
Once removed, you can reference the evicted document using the mutatedDocumentIDs method on the result.
Using Evict with Sync Subscriptions
Timing Subscriptions and Evictions
In addition, take a balanced approach when using the Subscribe and Evict methods; as in, consider the advantages and drawbacks of each method and use them as appropriate for the specific needs and requirements of your app.
Key considerations for using Subscription and Eviction methods include:
- Use Subscribe to sync more data across connected peers in the mesh; however, be mindful of potential increased network usage, which may degrade sync performance.
- Use Evict to manage local storage capacity and improve performance by routinely purging data stored locally.
Coordinating Evictions
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"EVICT FROM cars WHERE isSafeToEvict = true",
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);Soft-Delete Pattern
Adding a Soft-Delete Flag
To add a soft-delete pattern, set the isArchived field value to true:
{
"_id": "123",
"color": "blue",
"isArchived": true // add this field
}Querying Non-Archived Documents
To query to monitor documents that are NOT archived, establish a live query where isArchived is set to false, and then construct your live query callback.
For example, the following code demonstrates looking for documents that are not archived.
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"SELECT * FROM cars WHERE isArchived = false",
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);Removing Soft-Delete Flag
To remove the flag and reactivate the document, set the isArchived field to false:
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"UPDATE cars SET isArchived = false WHERE _id = '123'",
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);Considerations
Access Frequency and Relevance Considerations
In peer-to-peer system design, there are technical tradeoffs between the amount of data synced across peers and the timeliness of access to synced data:
- The greater the amount of data synced across connected peer devices, the more timely offline read access becomes. That is, database resilience in offline scenarios increases when there are more documents being synced across distributed peers.
- The fewer the number of documents replicated, the less the likelihood that peer devices run out of disk space and experience memory leaks, and the performance of the peer-to-peer mesh network that interconnects them degrades.
For considerations on using the Evict and Subscribe methods, see Timing Subscriptions and Evictions.
For advanced concepts related to design tradeoffs in distributed system architecture, see Cloud-Optional Design.