Why am I getting a 409 Conflict deleting a document through Couchbase Sync Gateway?


#1

Can anyone explain to me why I’m getting this response trying to delete a document?

curl -X DELETE --header 'Accept: application/json' 'http://localhost:4985/mydb/uprofile:testing'

When I run this, I get:

{
    "error": "conflict",
    "reason": "Document exists"
}

How come?


#2

you should add rev parameter
FYI
https://developer.couchbase.com/documentation/mobile/1.4/references/sync-gateway/rest-api/index.html#!/document/delete_db_doc

for example

curl -X DELETE --header 'Accept: application/json' 'https://localhost:4984/mydb/uprofile%3Atesting?rev={rev_info}'

#3

Does that mean I always need a rev ID to delete documents? The _id is not enough?


#4

yes. rev parameter is necessary. and it is only

Not used when creating a new document.


#5

For context, every update to a document including a delete request will result in a new revision added to the document’s revision tree - which is why it is necessary to specify the revision of the document that is to be deleted. If you are interested in what goes on behind the scenes , this blog may interest you https://blog.couchbase.com/conflict-resolution-couchbase-mobile/


#6

This should be specified in the API as it is currently listed as optional. Otherwise, if the revision is not specified, sync gateway should just use the latest revision. Seems like a bad API for people to have to know the revision if they just want to get rid of the document.


#7

Yes- The API spec will be updated. I have filed a ticket for that https://github.com/couchbaselabs/couchbase-mobile-portal/issues/742.
In order to understand why you need the “rev” parameter when you delete a document, I think it will help to understand how document revisions are managed in Couchbase. In a MVCC system , deletions are document updates so they should be treated the same way as a PUT.
Please check out the blog post link in my earlier response - see section on “Tombstones”.


#8

Thanks @priya.rajagopal. Do you think it is worth having another optional parameter, say deleteLatest, that can be set if the user just wants to delete the latest revision? Or isthis going against the idempotent nature of REST?


#9

hey - conceptually , a deleteLatest doesn’t quite work because the revision tree of a document can have conflicts/ multiple branches so “latest” isn’t really meaningful/deterministic. That’s why you would need to specify the parent rev because that’s the revision you would attach the tombstone revision to . Hope that clarifies. Of course, it also breaks the idempotency of PUT but the issue is more fundamental.