title: Auto delete .ts and .m3u8 files once client receives all .ts files
body: <p>So I created an express server that gets an mp3 file (which is stored locally right now, but will be taken from mongo db later) and uses ffmpeg to make .m3u8 and .ts files. The files are successfully sent to the client and there are no errors while playing it on the client. I used <a href="https://github.com/video-dev/hls.js/" rel="nofollow noreferrer">hls.js</a> to play these files in Chrome. But the server still has those files stored locally. Is there any way the server can know when to delete these files that it stored locally? There are a lot of files generated by ffmpeg so I can't just let them stay there forever.</p>
  <p>I used the ffmpeg part of code from <a href="https://www.npmjs.com/package/hls-server#producing-streams" rel="nofollow noreferrer">hls-server github repo</a>.</p>
  <h3>my server file</h3>
  <h1>index.js</h1>
  <pre><code>// just used to run ffmpeg for conversion
  var command = ffmpeg('inp.mp3')
    .on('start', function (commandLine) {
      console.log('command', commandLine);
    }).addOptions([
      '-c:a aac',
      '-b:a 64k',
      '-vn',
      '-hls_list_size 0',
      '-segment_time 10',
    ]).output('files\\output.m3u8');
  
  var express = require('express');
  var app = express();
  // express middleware to serve individual .ts and .m3u8 files when requested
  app.use(express.static('./files/'));
  
  app.get('/', function (req, res) {
    command.on('end', function () {
      console.log('done');
      res.write(`
      &lt;script src=&quot;https://cdn.jsdelivr.net/hls.js/latest/hls.min.js&quot;&gt;&lt;/script&gt;
      &lt;script&gt;
        function onLevelLoaded (event, data) {
          var level_duration = data.details.totalduration;
          console.log(level_duration, data);
        }
        if(Hls.isSupported()) {
          var audio = new Audio();
          var hls = new Hls();
          // requesting files from here
          hls.loadSource('http://localhost:8000/output.m3u8');
          hls.attachMedia(audio);
          hls.on(Hls.Events.LEVEL_LOADED, onLevelLoaded);
          hls.on(Hls.Events.FRAG_BUFFERED, (e, d) =&gt; {
            console.log(e, d);
          });
        }
      &lt;/script&gt;
      `);
      res.end();
    }).run();
  });
  
  app.listen(8000);
  </code></pre>