title: PouchDB delete document upon successful replication to CouchDB
body: <p>I am trying to implement a process as described below:</p>
  
  <ol>
  <li>Create a <code>sale_transaction</code> document in the device.</li>
  <li>Put the <code>sale_transaction</code> document in Pouch.</li>
  <li>Since there's a live replication between Pouch &amp; Couch, let the <code>sale_transaction</code> document flow to Couch.</li>
  <li>Upon successful replication of the <code>sale_transaction</code> document to Couch, delete the document in Pouch.</li>
  <li>Don't let the <code>deleted sale_transaction</code> document in Pouch, to flow through Couch.</li>
  </ol>
  
  <p>Currently, I have implemented a two-way sync from both databases, where I'm filtering each document that is coming from Couch to Pouch, and vice versa.</p>
  
  <p>For the replication from Couch to Pouch, I didn't want to let <code>sale_transaction</code> documents to go through, since I could just get these documents from Couch. </p>
  
  <pre><code>PouchDb.replicate(remoteDb, localDb, {
     // Replicate from Couch to Pouch
     live: true,
     retry: true,
     filter: (doc) =&gt; {
        return doc.doc_type!=="sale_transaction";
     }
  })
  </code></pre>
  
  <p>While for the replication from Pouch to Couch, I put in a filter not to let <code>deleted sale_transaction</code> documents to go through. </p>
  
  <pre><code>PouchDb.replicate(localDb, remoteDb, {
     // Replicate from Pouch to Couch
     live: true,
     retry: true,
     filter: (doc) =&gt; {
        if(doc.doc_type==="sale_transaction" &amp;&amp; doc._deleted) {
           // These are deleted transactions which I dont want to replicate to Couch
           return false;
        }
        return true;
      }
  }).on("change", (change) =&gt; {
     // Handle change
     replicateOutChangeHandler(change)
  });
  </code></pre>
  
  <p>I also implemented a change handler to delete the <code>sale_transaction</code> documents in Pouch, after being written to Couch.</p>
  
  <pre><code>function replicateOutChangeHandler(change) {
     for(let doc of change.docs) {
        if(doc.doc_type==="sale_transaction" &amp;&amp; !doc._deleted) {
           localDb.upsert(doc._id, function(prevDoc) {
              if(!prevDoc._deleted) {
                 prevDoc._deleted = true;
              }
              return prevDoc;
           }).then((res)=&gt;{
              console.log("Deleted Document After Replication",res);
           }).catch((err)=&gt;{
              console.error("Deleted Document After Replication (ERROR): ",err);
           })
        }
     }
  }
  </code></pre>
  
  <p>The flow of the data seems to be working at first, but when I get the <code>sale_transaction</code> document from Couch, then do some editing, I would then have to repeat the process of writing the document in Pouch, then let it flow to Couch, then delete it in Pouch. But, after some editing with the same document, the document in Couch, has also been deleted. </p>
  
  <p>I am fairly new with Pouch &amp; Couch, specifically in NoSQL, and was wondering if I'm doing something wrong in the process. </p>