30

How can I clear my DNS cache on my android phone? (Droid3, Gingerbread, rooted to be specific)

Ideally I'm looking for a command to run through a terminal emulator, something similar to

ipconfig /flushdns

On a windows machine.

What's the Linux version of this?

4
  • Do you need to do this for a system app, third party app or your app?
    – onik
    Sep 1, 2011 at 4:02
  • Third party I guess? I need it for when I ping a server via Terminal Emulator. Sep 1, 2011 at 4:30
  • Please update your title to reflect that you want a CLI-based answer. This is ranking in Google and 99% of people searching this title do not have root or CLI skills.
    – PJ Brunet
    Aug 11, 2015 at 16:09
  • Any update here? as @PJBrunet said, having root is not the usual case, moreover current phones do not allow to remove battery. I looking for flushing the DSN because doing it fixed my "ip failure" on windows, it is happening the same in my phone, but there is no shell on Android. Aug 1, 2020 at 17:13

7 Answers 7

11

On a typical Linux system the cache is cleared by running /etc/init.d/nscd restart, but at least my ROM doesn't use nscd to cache DNS. You can check if yours does, but I doubt it. I've seen suggestions that clearing the brower cache would clear DNS cache too, but one sure way is to do a hard reboot (shutdown, remove battery for 30s, reattach battery and boot).

3
  • 1
    All of my googling also brings up the "hard reboot" suggestion.
    – Chahk
    Sep 1, 2011 at 14:03
  • I'm downvoting because the answer requires root and/or assumes shell access which is unrealistic for most Android users. The rest of the answer did not solve my problem. Also I believe Matthew Read should reconsider locking the question because this is absolutely not a "done deal." DNS is extremely complex and there will be more legitimate answers from people without Android accounts here yet, I'm an example of that. FWIW I do have 10+ reputation and still could not answer the question.
    – PJ Brunet
    Aug 11, 2015 at 15:51
  • @PJBrunet To be accurate, you have 1 rep and 100 rep from association bonus, which does not count since you haven't earned it on this site. See What is a "protected" question? for more details. As for the question, I'll unprotect it for now.
    – onik
    Aug 11, 2015 at 15:54
15

Addresses are cached for 600 seconds (10 minutes) by default. Failed lookups are cached for 10 seconds. From everything I've seen, there's nothing built in to flush the cache. This is apparently a reported bug in Android because of the way it stores DNS cache. Clearing the browser cache doesn't touch the DNS, the "hard reset" clears it because it simply times out.

Can we ask the underlying reason for needing to clear the DNS cache? Perhaps there's another solution that's missed because we're not looking at the bigger picture.

3
  • 2
    Not sure about the OP, but I have a use case where my home network serves addresses like server.home.mydomain.com, but my ISP resolves *.mydomain.com (for any value of "*" this is not already mapped) to www.mydomain.com if my phone is on 3G/4G, so I'm sometimes unable to access my home network until the cache times out.
    – TomG
    Aug 25, 2012 at 1:25
  • Looks like the forced 10-minute caching is fixed in JB — see this commit. For older releases the Java cache behavior could be tuned by networkaddress.cache.ttl and networkaddress.cache.negative.ttl in build.prop. Jun 2, 2013 at 18:19
  • Fwiw, there are still some (perhaps different) DNS issues even in 4.3 - tablets on wifi may not get an IP address for names that Linux and Windows machines (on the network that hosts the wifi) can resolve. Feb 25, 2014 at 21:50
7

The only way to do this in Android is to do a hard reboot. The necessary command-line tools are not normally available, however in my tests a hard reboot has always done the trick for me (Galaxy Nexus, and HTC Desire, various ROMs).

This is a pain, but it is quicker than the 10min cache timeout.

3
  • How does one perform this? Nov 29, 2011 at 5:20
  • 1
    - Hold down the power button - Choose "Power off" - Allow phone to fully shut down - Remove battery and wait 30s - Return battery - Power phone back on. In my experience removing the battery is not necessary, but some have reported it doesn't work if you miss this step.
    – Martin
    Nov 29, 2011 at 8:27
  • 4
    Removing the battery is nonsense. Reboot works. I suspect that the additional time for that simply allows some external DNS cache to time out. Sep 27, 2016 at 19:52
5

There're also apps like Internet Booster promising to "clear DNS cache" (amongst other things). I didn't try it out myself, and furthermore there seems no way to do only that (just one "optimize" button which "applies improvements"); also its effects might differ between devices (says the app's description) -- but it might be better than a reboot. Btw: while not mentioning root as requirement, it might still be needed.

There are several "similar apps" listed on Google Play -- but be aware that most of them include Airpush ads and/or want excessive permissions, which is why I didn't name them here.


Another possible solution (not tested by myself) I found at MadPC, seemingly quoting user8522's answer before stating:

The last option but most intensive on the user themselves unless they want to just wait the update out: Have your users go into settings -> applications -> Network Location -> Clear data

Another short howto mentions a different easy way to flush DNS (again, I didn't test this myself): Simply switch to airplane mode (oops, no network?) and back (ah, there it is) should do the trick. Might sound stupid, but I could imagine when entering airplane mode Android does something similar to /etc/init.d/networking stop, which has a DNS flush as side-effect.


A very easy solution was supplied by dtumaykin in the comments below: simply switching to airplane-mode and back might already do it (of course with the side-effect of the network going down for a couple of seconds ;) Though it might not work on some devices, it's worth a try: no extra installs, and easily performed even by n00bs.

If anybody tried one of those solutions, confirmation (or the opposite) is surely welcome :)

7
  • 3
    There are shell commands ndc resolver flushdefaultif and ndc resolver flushif wlan0; they require root. There is also NetworkManagementService.flushDefaultDnsCache() method, which required just CHANGE_NETWORK_STATE before JB, but then was locked down to CONNECTIVITY_INTERNAL. So there apparently was a hackish way to clean DNS cache without root (after build.prop tuning to kill the Java-level cache), but it will not work in JB. Jun 2, 2013 at 19:20
  • Thanks for the additional information, @SergeyVlasov ! Do you know if any of the methods you mentioned gets triggered by the two proposed solutions mentioned last in my answer (or if they do work, but for a different reason)?
    – Izzy
    Jun 2, 2013 at 19:37
  • 5
    Airplane mode on-off worked out for me. Samsung Galaxy S4 Mini and CM12.1.
    – dtumaykin
    Jul 4, 2015 at 12:45
  • I can't answer because Matthew Read locked the question. Please slap him for me :-) Hard reboot did not work for me, airplane mode did not work, clearing "location" (as per answer) did not work, waiting hours did not work either. In the past, I've simply solved this by closing the browser tab and opening a new tab. There is a "no root" 8.8.8.8 app (Google DNS) however, I can't grant VPN access because I already have a VPN app that I paid for. The solution for me was change the VPN's "virtual location" setting or the solution was to stop & start my VPN. Assuming my VPN has its own DNS cache.
    – PJ Brunet
    Aug 9, 2015 at 21:20
  • @PJBrunet I won't slap a mod (too dangerous, he might close my account ;) and besides, my arms are not long enough to reach oversea). But gaining 10 rep isn't too hard (ask a good question getting 2 upvotes, or write an answer getting 1 upvote); with that, come back here and write your answer :) Thanks for sharing!
    – Izzy
    Aug 10, 2015 at 8:43
2

In modern versions of Android there is an option to clear Chrome's DNS cache under chrome://net-internals/#dns

1
  • didn't work for me, still had to do a hard reboot
    – Cerveser
    Jul 8, 2016 at 19:38
1

In my case, I just had one domain name that I needed flushed. Suggestions such as toggling Airplane Mode, performing a hard reboot, or the actions in chrome://net-internals/#dns did not work for me. Instead I temporarily changed my WiFi settings to use Google's nameserver at 8.8.8.8.

These are the steps I took:

  • Settings -> WiFi -> tap current connection and note the current IP address
  • Long click current connection -> Modify network
  • Check "Advanced settings"
  • Change "IP settings" to "static"
  • Set "IP address" to the current IP from the first step
  • Set "Gateway" to your IP address but with .1 as the 4th number (This is just a guess. If you know it is something different, use that)
  • Set "DNS 1" to 8.8.8.8

After these steps, when I pinged the domain name in the console it resolved to the correct IP address and seemed to write over the old cached one. Presumably visiting the domain in a browser will have the same effect.

When I went back to WiFi and set "IP settings" back to "DHCP", the correct domain continued to resolve to the current IP address.

This is on a Sony Xperia Z3 running Android 5.1.1

0

On a Adroid 5.1 Mobile which has took wrong DNS as is switched to much between two WLAN;( A question Mark on a WLAN Connexion. i did not want to restore my phone as i did it once.. Engelsiz: DNS Changer fixed it;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .