Skip to main content

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.

Remove by _id

Removing by document _id is permanent. You can not create a new document with the same _id at a later time.

await ditto.store.collection('people').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 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 of more documents from the local on-disk storage.

await ditto.store.collection('people').find('age <= 32').evict()