title: MongoDB files and collection Purge
body: <p>We are using MongoDB files and chunks for storing images and pdf files that are larger than 16 MB in size. We have a requirement to purge this data at the end of the day.</p>
  <p>We found that MongoDB provides an inbuilt feature to delete the docs using a TTL index. So defined that TTL index using the uploadDate column of the files collection. It is working perfectly and deleting the records from files collection. But the related records in the chunk collection remain undeleted which is problematic.</p>
  <p>Another option we thought of adding uploadDate field in chunk collection and defining TTL index there as well but could not find a way to achieve it. I see that we can add a metadata field in files collection but did not find a way to do it in chunk collection.</p>
  <p>Another option we discussed is - since we have a spring boot microservice, we can have a spring scheduler to invoke at defined intervals and call a service method that will invoke the delete() gridFS API for deleting the expired documents. But since we can have multiple instances of this microservice with a common database then each instance will end up running its own scheduler service for the same thing which does not seem to be right.</p>
  <p>If you come across such a scenario and know of achieving this then that will help me.</p>
  <p>Thanks</p>