Removing
Removes will delete or remove one or more documents from the collection. Calling remove will sync removals to the local device and to other devices as well.
danger
Removing documents will remove documents from the local device and other
peers. Your application code should consider remove
calls to be destructive.
In other words, if you call remove on document, your application should consider
this document gone throughout your system.
In the v4 version of Ditto, any device can “undo” the remove by calling upsert on the document with _id=abc123
again.
collection.findByID(docID).remove()
Soft-delete documents
Consider using a "soft-delete" pattern in your application. You should use a
field like isArchived: true
instead of calling remove()
. You can query and
synchronize only the documents that have not been marked archived. Using a
soft-delete pattern, documents can be synced or updated again if necessary.
{ "_id": "123abc", "name": "Foo", "isArchived": true // add this field}
And query non-archived documents like so:
let liveQuery = ditto .store.collection('cars') .find('!isArchived').observeLocal((documents) => { console.log('these are the unarchived documents', documents) })
You can easily un-archive these documents by calling update on that field:
ditto.store.collection('cars').update((mutableDoc) => { mutableDoc["isArchived"] = false})
Evicting documents
Once you archive a document, you can delete it from the local device to free up disk space. This operation is different than remove in that it only removes one or more documents from the local on-disk storage.
collection.find("owner == 'Bob'").evict()