Q:

Use update handler for document delete

I need to delete a document from CouchDB, but first I need to check if a condition is met. The first thing that comes to mind is using an update handler instead, iterating manually over all keys in the update handler and deleting those without a _ prefix and setting the _delete field to true.
I guess this should produce the same result as a normal DELETE, am I right?
Update:
Using @Kxepal's suggestion, this is what the update handler looks like:
function (doc, req) {
  if (req.query.account_id != doc.account_id) {
    return [doc, '{"error": "invalid account id"}'];
  }
  return [{
    _id: doc._id,
    _rev: doc._rev,
    _deleted: true
  }, 'ok'];
}

A:

Not really.
When you DELETE document, CouchDB removes any fields from it except _id and _rev (sure, _deleted is also there). This information is also known as tombstone.
When you just adds _deleted: true to document, it becomes marked as deleted, but all other fields are still there:
http --json http://localhost:5984/recipes/SpaghettiWithMeatballs:
HTTP/1.1 404 Object Not Found
Cache-Control: must-revalidate
Content-Length: 41
Content-Type: application/json
Date: Sat, 17 Aug 2013 14:49:17 GMT
Server: CouchDB/1.4.0+build.c843cef (Erlang OTP/R16B)

{
    "error": "not_found", 
    "reason": "deleted"
}

http --json http://localhost:5984/recipes/SpaghettiWithMeatballs\?rev\=9-ac84157c2206793b7d142f5d8e2c97d0:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 425
Content-Type: application/json
Date: Sat, 17 Aug 2013 14:48:55 GMT
ETag: "9-ac84157c2206793b7d142f5d8e2c97d0"
Server: CouchDB/1.4.0+build.c843cef (Erlang OTP/R16B)

{
    "_attachments": {
        "my_recipe.txt": {
            "content_type": "text/plain", 
            "digest": "md5-198BPPNiT5fqlLxoYYbjBA==", 
            "length": 85, 
            "revpos": 2, 
            "stub": true
        }
    }, 
    "_deleted": true, 
    "_id": "SpaghettiWithMeatballs", 
    "_rev": "9-ac84157c2206793b7d142f5d8e2c97d0", 
    "description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.", 
    "ingredients": [
        "spaghetti", 
        "tomato sauce", 
        "meatballs"
    ], 
    "name": "Spaghetti with meatballs"
}

However, during compaction, this document will be converted to tombstone with only _id, _rev and _deleted fields. Until then, this is good trick to keep data within document to easily undo delete operation without requesting document's previous revision since all content available for the latest one.

