title: Rails & postgres - reindex after restore
body: <p>Recently I ran into a situation where I had to restore a backup database from Heroku to my local setup because of a configuration error I had made.</p>
  
  <p>This was very straight-forward from a Heroku post:</p>
  
  <p><a href="https://devcenter.heroku.com/articles/heroku-postgres-import-export" rel="nofollow">https://devcenter.heroku.com/articles/heroku-postgres-import-export</a>:</p>
  
  <pre><code>$ heroku pgbackups:capture
  $ curl -o latest.dump `heroku pgbackups:url`
  $ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
  </code></pre>
  
  <p>The issue became that after the restore, some of the old (pre-restore) index data was still being served with the newly restored data.  i.e. it seemed that there was still some remnant index data after the restore and a db migration.  Note that before the restore, I did not clear or delete or do anything to my old db ... so obviously there was some data still there pre-restore ... the tables were gone, but the indexes may have still been around.</p>
  
  <p>I suspected that if I made some changes to the table, that would cause a partial reindex of whatever I changed, and would be some type of partial fix to the problem.  And sure enough, when I made a change to the record via the console, the remnant data was updated (and was now correct).</p>
  
  <p>So my question is multi-part ...</p>
  
  <p>Why did the restore not clear everything in the db and "start from scratch"?  (Is there another way?)</p>
  
  <p>And is there a better programatic way to reindex a table via rake or console?</p>
  
  <p>For searchkick I use:</p>
  
  <pre><code>rake searchkick:reindex CLASS=Blog
  </code></pre>
  
  <p>And within models I use something like:</p>
  
  <pre><code>blog.reindex
  </code></pre>
  
  <p>But I have not found a command/function way to do a basic table reindex (without some type of hack to touch every record).</p>