Platform Manual
CRUD Operations
Evicting and Removing
data storage management is essential for preventing unnecessary resource usage, which affects not only performance but also battery life and overall end user experience having a well thought data storage strategy ensures that your end user environment remain memory efficient and, as a result, your app more performant — faster load times, improved battery life, a responsive user interface, and so on /#considerations /#evict method /#remove method /#soft delete flag considerations depending on your use case, use either the evict or remove methods, as well as applying soft delete patterns to implement tools to help optimize memory usage in your app to mitigate the risk of memory leaks, performance degradataion, crashes, data loss, and, if applicable, reduced battery life, it is critical that you implement a thoughtful memory management strategy in your app when planning your approach to memory management in your app, use the following criteria to help you during the decision making process consideration recommendation access frequency and relevance ensure memory is allocated only to the most relevant and frequently accessed documents by establishing an automatic process that evicts documents that are accessed less frequently no longer relevant or needed time based data establish an automatic process to evict or remove time based data older than a minimum of seven days (until expired, time based data remains accessible by way of local queries ) permanent data loss apply careful consideration for your specific use case before invoking the remove method to delete for instance, removing documents may be useful for a scenario where your end user wants to remove a specific message from a chat conversation, such as a message they've previously sent 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 docid 2xcufjiq7l5ttpfxvuia6 evict method the evict method, once invoked, immediately removes the specified document from the local ditto store, making it inaccessible by local queries for complete dql syntax, see docid\ kspbi9cshpdud0iyuow9e although the document you evicted is removed from the local ditto store, the document stored within remote ditto stores persists to prevent the evicted data from reappearing on the screen in a single flicker, make sure to stop subscriptions before you call evict ; otherwise, the subscription remains active and even if you reset the data in your end user environment, the evicted data momentarily reappears ditto store collection("your collection name") find(\[query]) evict()ditto store\["your collection name"] find(\[query]) evict()await ditto store collection("your collection name") find(\[query]) evict()ditto store collection("your collection name") find(\[query]) evict();ditto store collection("your collection name") find(\[query]) evict()ditto get store() collection("your collection name") find(\[query]) evict()let collection = ditto store() collection("your collection name") unwrap(); collection find(\[query]) evict() unwrap(); using evict for local and live queries the following snippets illustrates fetching documents with the field property color set to the 'blue' within the cars collection once retrieved, ditto automatically triggers the evict callback function to perform the specified purge ditto store collection("cars") find("color == 'blue'") evict()ditto store collection("cars") find("color == 'blue'") evict()await ditto store collection("cars") find("color == 'blue'") evict()ditto store collection("cars") find("color == 'blue'") evict();ditto store collection("cars") find("color == 'blue'") evict()ditto get store() collection("cars") find("color == 'blue'") evict()cars find("color == 'blue'") evict() unwrap(); for more information, see docid\ hvfmp8tx4cwki4h2xotuy using evict for replication queries to clear documents with active subscriptions, you must first cancel the relevant subscription before calling the evict method in your code you must manage subscriptions and evictions carefully if subscriptions are not properly managed prior to executing evictions, you may inadvertently disrupt the intended state, resulting in inconsistencies and unexpected behavior for instance, the eviction process failing and the document persisting in the local ditto store for example, if you have an active subscription for fetching 'blue' cars and you subsequently evict the document with the id '123456' that matches the replication query, connected peers reinstate it in your local ditto store; the document will not be cleared from the local ditto store timing subscriptions and evictions the frequency for removing locally stored documents depends on your app's use case to avoid the risk of depleting local storage capacity, consider evicting data frequently, such as once per day (if not more) to enhance offline datastore resiliency, you can implement app logic that allows your end users to choose which data to evict from their environments 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, but be mindful of potential increased network usage that may degrade performance use evict to remove data stored locally in an effort to manage local storage capacity and improve performance forcing evictions if you want to indicate that a batch of documents are irrelevant and, although they are to be retained, should not sync across peers, add the issafetoevict field to the document property tree then, use a method to alert clients to flag any documents they consider irrelevant ditto document { " id" "abc123", "color" "red", "mileage" 40000, "issafetoevict" true, "createdat" "2023 05 22t22 24 24 217z" } to ensure that peers continue replicating documents that are considered relevant, incorporate issafetoevict == false into their sync subscription query this approach restricts replication only to documents that peers mark as 'true' for issafetoevict once flagged, the peers clear irrelevant documents from their caches, all the while normal transactional operations continues without interruption collection find("issafetoevict == false") subscribe()collection find("issafetoevict == false") subscribe()collection find("issafetoevict == false") subscribe()collection find("issafetoevict == false") subscribe()let livequery = ditto store collection('cars') find('!issafetoevict') observelocal((documents) => { console log('these are the unarchived documents', documents) })collection find("issafetoevict == false") subscribe()collection find("issafetoevict == false") subscribe() remove method use the remove method when you want to permanently delete the document from the entire ditto platform; that is, your local ditto store as well as all the remote ditto stores connected in the mesh network invoking the remove method results in irreversible data loss once a document is removed, it is permanently eliminated throughout the ditto system and can never be restored; however, as of ditto version 4 0 release, if a document was previously removed, you can reverse the removal for more information, see docid\ q6vsbhczgypt1hlzgow4e removing documents once you remove a document, the data it contains is wiped and a tombstone metadata is affixed to signal to remote peers that the document is deleted and no longer available the absence of a document does not indicate that it has been deleted from the ditto store; only the tombstone metadata marker added to the document structure indicates its deletion from the platform remove may be useful for a scenario where your end user wants to remove a specific message from a chat conversation, such a message sent collection findbyid(docid) remove()collection findbyid(docid) remove()await ditto store collection("your collection name") findbyid("unique document id") remove()ditto store collection("your collection name") findbyid("unique document id") remove();ditto store collection("your collection name") findbyid("unique document id") remove()ditto get store() collection("your collection name") find by id("unique document id") remove()collection find by id(id) remove() unwrap(); for example, the following snippet illustrates locating the document assigned the primary key 123456 from the cars collection once retrieved, ditto automatically executes the removal process let carscollection = ditto store collection("cars") // remove the document with id "123456" from the collection if let document = carscollection findbyid(123456) { do { try document remove() } }val carscollection = ditto store collection("cars") try { carscollection findbyid(123456) remove() }await ditto store collection("cars") findbyid("123456") remove()dittocollection carscollection = ditto store collection("cars"); // remove the document with id "123456" from the collection dittodocument document = carscollection find("id == '123456'");ditto store collection("cars") findbyid("123456") remove()ditto get store() collection("cars") find by id("123456") remove()cars findbyid("123456") remove() unwrap() soft delete flag if you need a data recovery option, instead of permanently removing the data from the local ditto store like evict , opt for a soft delete pattern a soft delete pattern is a way to flag data as inactive while retaining it for various requirements, such as archival evidence, reference integrity, prevention of potential data loss due to end user error, and so on adding a soft delete flag to add a soft delete pattern, set the isarchived field value to true json { " id" "123abc", "name" "foo", "isarchived" true // add this field } querying non archived documents to query to monitor documents that are not archived, establish a live query using the find method enclosed with !isarchived , and then construct your live query callback for example, the following code demonstrates looking for documents that are not archived once found, the documents output, or log , to your console let livequery = ditto store collection('cars') find('!isarchived') observelocal((documents) => { console log('these are the unarchived documents', documents) }) removing soft delete flag to remove the flag and reactivate the document, set the isarchived field to false ditto store collection('cars') update((mutabledoc) => { mutabledoc\["isarchived"] = false })