Skip to main content

How to Flush the DNS Cache in Batch Script

The DNS (Domain Name System) cache is a temporary database on your computer that stores recent DNS lookups. This allows your machine to quickly resolve a domain name (like www.google.com) to an IP address without having to ask an external DNS server every time, which speeds up your browsing experience. However, this cache can sometimes become outdated or corrupt, leading to "Page Not Found" errors or problems connecting to a website that has recently changed its IP address.

This guide will teach you how to use the standard, built-in ipconfig command to clear or "flush" the DNS resolver cache. This is one of the most common and effective first steps in troubleshooting any network connectivity issue.

The Core Command: ipconfig /flushdns

The ipconfig utility is the primary command-line tool for managing your computer's network configuration. It has a specific switch designed for one purpose: to completely clear the DNS cache.

Syntax:ipconfig /flushdns

This command has no other options. It simply targets the local DNS cache and empties it. This is a privileged operation and requires an elevated command prompt.

Basic Example: A Simple DNS Flush Script

This is a simple one-line script that executes the command. It's often useful to have this on your desktop for quick network troubleshooting.

warning

Important: You must run this script as an Administrator for it to work.

@ECHO OFF
TITLE DNS Cache Flusher
ECHO --- Flushing the DNS Resolver Cache ---
ECHO.

ipconfig /flushdns

ECHO.
ECHO --- Operation complete ---
PAUSE

If successful, the command will print a confirmation message.

--- Flushing the DNS Resolver Cache ---

Windows IP Configuration

Successfully flushed the DNS Resolver Cache.

--- Operation complete ---
Press any key to continue . . .

How to Verify that the Cache was Flushed

How do you know the command actually worked? You can check the contents of the cache before and after running the flush command using ipconfig /displaydns.

@ECHO OFF
ECHO --- DNS Cache Before Flushing ---
ipconfig /displaydns | find "Entries"
PAUSE

ECHO.
ECHO --- Flushing Now ---
ipconfig /flushdns

ECHO.
ECHO --- DNS Cache After Flushing ---
ipconfig /displaydns | find "Entries"
PAUSE
note
  • The | find "Entries" is a trick to get the summary count on some systems, but the key is that the "After" list will be empty or drastically smaller.
  • The "After" command will typically report: Could not display the DNS Resolver Cache.

How the Command Works

The ipconfig /flushdns command sends a signal directly to the DNS Client service (dnscache) running on your Windows machine.

  • This service is responsible for performing and caching DNS lookups.
  • The command instructs the service to immediately discard all of the records it is currently holding in memory.
  • The next time you try to visit any website, your computer will be forced to perform a fresh DNS lookup from your configured DNS server, ensuring you get the most up-to-date IP address.

Common Pitfalls and How to Solve Them

Problem: The Script is Not Run as Administrator

This is the number one reason the command fails. Modifying the DNS cache is a system-level operation.

Example of error message:

The requested operation requires elevation.

Solution: The script must be run as an Administrator. Right-click your .bat file or cmd.exe and select "Run as administrator." There is no workaround.

Problem: The DNS Client Service is Not Running

The ipconfig /flushdns command talks to the dnscache service. If this service is stopped or disabled, the command will fail.

Example of error message:

Could not flush the DNS Resolver Cache: Function failed during execution.

Solution: This is rare, as the service is set to run automatically by default. However, a robust script could check for and start the service if needed.

SC query dnscache | FIND "STATE" | FIND "RUNNING" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO DNS Client service is not running. Starting it...
NET START dnscache
)

Practical Example: A Full Network Reset Script

Flushing the DNS is often just one step in a series of commands to completely reset a computer's network state. This script combines it with releasing and renewing the IP address.

@ECHO OFF
SETLOCAL
TITLE Full Network Reset Utility
REM This script must be run as an Administrator.

ECHO --- Network Reset Script ---
ECHO This will release and renew your IP, and flush your DNS cache.
ECHO Your network connection will be briefly interrupted.
ECHO.
PAUSE

ECHO Step 1: Releasing current IP address...
ipconfig /release

ECHO.
ECHO Step 2: Renewing IP address from DHCP server...
ipconfig /renew

ECHO.
ECHO Step 3: Flushing the DNS Resolver Cache...
ipconfig /flushdns

ECHO.
ECHO [SUCCESS] Network reset is complete.
PAUSE
ENDLOCAL

Conclusion

The ipconfig /flushdns command is an essential tool for any network administrator or power user. It provides a simple and effective way to resolve many common name resolution issues.

Key takeaways for using it in a script:

  • You must run the script as an Administrator.
  • The command ipconfig /flushdns is all you need to clear the cache.
  • You can verify the operation by using ipconfig /displaydns before and after.
  • It is often used as part of a larger network reset sequence, along with ipconfig /release and ipconfig /renew.