title: How to replace options in a Select2 select box
body: <p>I'm trying to dynamically clear a <a href="https://select2.github.io/" rel="nofollow noreferrer">Select2</a> option list, and reload it with new data. Everything I read tells me that I can set the data like I'm doing, but it only appends data, and doesn't clear it.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$("#optioner").select2();
  
  $("#doit").click(function() {
    $("#optioner").select2({
      data: [{
        id: "real1",
        text: "Real 1"
      }, {
        id: "real2",
        text: "Real 2"
      }]
    });
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" /&gt;
  &lt;link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" /&gt;
  
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"&gt;&lt;/script&gt;
  
  &lt;h1&gt;Demo&lt;/h1&gt;
  
  &lt;select id="optioner" class="select2"&gt;
    &lt;option value="fake1"&gt;Fake 1&lt;/option&gt;
    &lt;option value="fake2"&gt;Fake 2&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;input id="doit" type="button" value="Real It Up"&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I <a href="https://jsfiddle.net/Kieveli/Lhhe8t44/" rel="nofollow noreferrer">fiddled that up</a> for yall.</p>