title: Set TTL for documents in Couchbase Server
body: <p>I want to set TTL (time to live) at couchbase server for all documents getting pushed from mobile devices continuously for replication. I did not find any clear explanation/example in documentation to do this.
  What should be the approach to set TTL for documents coming from mobile devices to Server through Sync Gateway.</p>
  
  <p>Approach 1:
  One approach is to create a view at server side which would return createdDate as key. We will query that view for keys of today date which would return today documents and we can set TTL for those documents. But how and when would we call this view and is it a good approach?</p>
  
  <p>Approach 2:
  Should I do it by using webhooks where it will listen to document changes (creations) made through Couchbase Lite push replications, set TTL for new documents and save back to Couchbase server?</p>
  
  <p>Is there any other better way to do it?</p>
  
  <p>Also what is the way to set TTL for only specific documents?</p>
  
  <p>EDIT: My final approach:
  I will create following view at couchbase server:</p>
  
  <pre><code>function (doc, meta) {
    emit(dateToArray(doc.createdDate));
  }
  </code></pre>
  
  <p>I will have a job which would run daily at EOD, query view to get documents created today and set TTL for those documents.
  In this way I would be able to delete documents regularly. </p>
  
  <p>Let me know if there is any issue with it or there is any better way.</p>