Clearing Safari DNS Cache

If you are using Safari and work with local sites, it can happen that you get redirected to the wrong place. Usually when this happened to me, the problem was that I changed the URI of a local site but the DNS cache still got the old outdated one stored and therefore redirected to the wrong place.

When this happened, Safaris DNS cache seemed to be pretty stubborn, ignoring all my attempts to get it to change its mind and learn to redirect to the new address instead. I had a few times where I just gave up and switched browsers for a while, to be able to access a local site again. After a while it will recover, resetting its cache and redirect properly again.

But there is actually a solution that seems to be working so far. And because I’ve ran into this problem and searched for this same solution way too many times, I thought I’ll add this here as a note to myself for the next time I run into it.

Ok, so you got yourself redirected out of your own local environment. Now what?

Here’s what you can do to clear Safaris DNS Cache:

  1. Close Safari
  2. Open a command line and run sudo dscacheutil -flushcache
  3. Then, run sudo killall -HUP mDNSResponder
  4. Open Safari again, go to the Developer Menu and run “Empty Caches” to clear all caches (not sure if needed, but just in case)
  5. Close and open Safari again
  6. Now open your desired local address like https://foo.localhost

Voilà. Your site should now be loading correctly again.

This worked for me so far on Mac OS 13.5.1 (Ventura) and Safari 16.6 (18615.3.12.11.2)

Made with ❤️ in Switzerland