I'm building a command line application using ActiveRecord 3.0 (without rails). How do I clear the query cache that ActiveRecord maintains?
6 Answers
To a first approximation:
ActiveRecord::Base.connection.query_cache.clear

- 44,205
- 11
- 83
- 107

- 41,404
- 5
- 117
- 189
Have a look at the method clear_query_cache
in http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/QueryCache.html
-
I have seen the method. However, calling it on MyModel.connection doesn't seem to be having any effect. I am doing mass inserts and need to clear the query cache so that ActiveRecord can see the new records, but so far I have not been successful. – clacke Oct 20 '11 at 18:11
-
1It seems that in Rails 2 you could reach the query cache through `ActiveRecord::Base.query_cache.clear_query_cache`, but now that place is gone. Like I said, doing it on the connection of the model does not work. Finally I solved it by going completely behind AR's back also for the queries following that mass insert. – clacke Oct 20 '11 at 19:39
-
@clacke can you post the final solution you found ("completely behind AR's back")? Or are you indirectly saying your using SQL instead of AR? – jvatic Jan 04 '12 at 20:42
-
2See ActiveRecord::Base#reset_column_information: http://apidock.com/rails/ActiveRecord/Base/reset_column_information/class – jvatic Jan 04 '12 at 20:48
-
@jvatic Yes, in the end I was using SQL instead of AR for both loading and storing. I never considered using `#reset_column_information`, maybe that clears the records cache as well. Will look at that next time. – clacke Jan 30 '12 at 05:02
We use:
ActiveRecord::Base.connection.query_cache.clear
(ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
table.classify.constantize.reset_column_information rescue nil
end
But I am not certain even this is enough.

- 128,308
- 78
- 326
- 506
If you only want to do this temporarily, you can use ActiveRecord::Base.uncached
like so:
::ActiveRecord::Base.uncached { User.order('random()').limit(3) }

- 10,936
- 8
- 64
- 79
Oftentimes when you see caching of database queries, your db is doing the caching, not ActiveRecord, which means you need to clear the cache and buffers at the db level, not the ActiveRecord level.
For example, to clear Postgres' cache and buffers on Mac, you would do sudo purge
, which forces the disk cache to be flushed and emptied.
To clear Postgres' cache and buffers on Linux, you would shut down postgres, drop the caches, and start postgres back up again:
service postgresql stop
sync
echo 3 > /proc/sys/vm/drop_caches
service postgresql start
Further reading:

- 11,283
- 7
- 55
- 61
-
1Why drop the page, dentry, etc. cache? That's a terrible idea usually, even if we ignore rightly or not opiniated kernel developers, especially for the rest of the system. This busted their caches! Whether it's `ls` again in this directory, or your instance of Redis or whatnot. The Linux file cache usually works, and not only that, but what simplistic queries that typical Rails apps perform are cached by PG? And even then, are we assuming that this PG instance has no other consumers in need of any perf? Rails caches singles fetches, and to some extent associations. That's the bulk. – pilona May 30 '19 at 20:02
Since, the title of the question is so broad, I stumbled over it while searching for a related problem: When adding columns to an ActiveRecord model during a migration, it may happen that the new column is not available on the ActiveRecord class. Rails caches the column information. In order to fix this we need to call reset_column_information
.
Here and example:
# migration
Product.first # rails caches the schema
add_column :products, :name
Product.first.update(name: 'xxx') # fails
Product.reset_column_information
Product.first.update(name: 'xxx') # now it succeeds

- 1,638
- 18
- 18