Question about document deletion


#1

I was reading the Couchbase (lite) Native API documentation on Documents and still had a couple of questions.

If I am using the .delete function, the document is now tombstoned, will I still be able to pull the document out and read its properties? Also, will it be purged eventually (i.e. If the document is left untouched I cannot for sure assume I will always be able to view the contents)?

If I am using the manual deletion function (setIsDeletion() or .isDeletion to true), and then updating the fields (to record down who delete it and when), will I still be able to pull the document out and read it’s properties? Also, will it be purged eventually (i.e. If the document is left untouched I cannot for sure assume I will always be able to view the document contents)?


#2

A ‘tombstone’ or deletion marker is just a revision that has the special property _deleted with a value of true.

The regular deleteDocument method just adds a new revision whose properties are {"_deleted":true} and nothing else. So the rest of the document’s properties no longer exist in that revision.

Until the database is compacted, you can get the parent revision, from before you deleted the doc, and read its properties. But compaction removes the bodies (JSON) of non-current revisions, so after that happens the properties are gone.

Deletions are not purged. The tombstone revision will stay around indefinitely, until/unless you purge it manually. (This serves as a reminder to the replicator that you already know about that document; otherwise a later replication might download the tombstone again.)

If I am using the manual deletion function (setIsDeletion() or .isDeletion to true), and then updating the fields (to record down who delete it and when), will I still be able to pull the document out and read it’s properties?

Yes, if you create the deletion revision yourself, any other properties in that revision can be accessed normally. (They also get replicated, so those properties will show up on the server or on other clients, which can be useful.)


#3

Thank you for your help! I think I kinda got it now…

So basically manual deletion and “auto” deletion results in the same thing, which is our properties data being removed, leaving their internal data (which begins with _) intact, and also that previous revisions (except itself, i.e. the new revision that is now marked as deleted) could be removed during a purge/compaction. And that in manual deletion we can manually add properties to the now empty JSON data. I would assume we can’t retain our properties data AND add the _delete flag to true, but we would be able to workaround that by manually adding the flag and then add back our properties data?


#4

I would assume we can’t retain our properties data AND add the _delete flag to true

Sure you can. Just update the document, and set the deletion property of the UnsavedRevision you’re updating.