title: Couchbase deleted documents reappearing in database
body: <p>We are experiencing a problem where deleted documents are reappearing on our Couchbase server.</p>
  <p>We have a scenario where documents are created on CBL. These documents are synced up to the server. The user realizes an error has been made and flags the document as incorrect. On the server the admin can then view all of the flagged documents and delete them from the server. The sync gateway has been setup to only sync up these types of documents, i.e. once an edit has been made to these documents on the server the changes are not synced back down to CBL.</p>
  <p>Here is the process of what is happening:</p>
  <ol>
  <li>Document is created on CBL with a TTL of 15 days and synced to sync</li>
  <li>Document is updated on CBL and synced to sync gateway.</li>
  <li>Document is deleted from the Couchbase Server bucket with a DELETE
  N1QL query.</li>
  <li>After the document is deleted from the bucket it gets
  randomly added again within a few days.</li>
  <li>Only documents that are
  still on the devices i.e. not older than TTL of 15 days, are added
  back to the bucket.</li>
  </ol>
  <p>We tried increasing the Metadata Purge Interval to more than 15 days but this did not resolve the problem.</p>
  <p>Does anybody have any suggestions or possibly know what could be the problem here?</p>
  <ul>
  <li>Couchbase Server Community Edition 6.5.1 build 6299</li>
  <li>Sync gateway 2.7.3</li>
  <li>Couchbase Lite Android 2.8.1</li>
  </ul>
  <p>Thanks in advance!</p>
  <p>PS: Here is our Sync Gateway config with the sync function:</p>
  <pre><code>    &quot;log&quot;: [
          &quot;*&quot;
      ],
      &quot;adminInterface&quot;: &quot;0.0.0.0:4985&quot;,
      &quot;interface&quot;: &quot;0.0.0.0:4984&quot;,
      &quot;databases&quot;: {
          &quot;prod&quot;: {
              &quot;server&quot;: &quot;http://localhost:8091&quot;,
              &quot;bucket&quot;: &quot;prod_bukcet&quot;,
              &quot;username&quot;: &quot;sync_gateway&quot;,
              &quot;password&quot;: &quot;XXX&quot;,
              &quot;enable_shared_bucket_access&quot;: true,
              &quot;import_docs&quot;: &quot;continuous&quot;,
              &quot;use_views&quot;: true,
              &quot;users&quot;: {
                  &quot;user_X&quot;: {
                      &quot;password&quot;: &quot;XXX&quot;,
                      &quot;admin_channels&quot;: [&quot;*&quot;],
                      &quot;disabled&quot;: false
                  }
              },
              &quot;sync&quot;:`
                  function sync(doc, oldDoc) {
                      /* sanity check */
                      // check if document was removed from server or via SDK
                      // In this case, just return
                      if (isRemoved()) {
                          return;
                      }
                      //Only sync down documents that are created on the server
                      if (doc.deviceDoc == true) {
                          channel(&quot;server&quot;);
                      } else {
                          if (doc.siteId) {
                              channel(doc.siteId);
                          } else {
                              channel(&quot;devices&quot;);
                          }
                      }
                      // This is when document is removed via SDK or directly on server
                      function isRemoved() {
                          return (isDelete() &amp;&amp; oldDoc == null);
                      }
                      function isDelete() {
                          return (doc._deleted == true);
                      }
                  }`,
          }
      }
  }
  </code></pre>