DNS caching speeds up domain resolution but can cause headaches during development when testing domain changes, switching servers, or debugging network issues. As developers, we frequently need to force our systems to fetch fresh DNS records.
For Windows 10/11 and Server editions, use Command Prompt with admin rights:
ipconfig /flushdns
For PowerShell (recommended for scripting):
Clear-DnsClientCache
The command varies by macOS version. For modern versions (10.15+):
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
For scripting purposes, consider this Bash function:
flush_dns() {
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
echo "DNS cache flushed"
}
Linux implementations vary by distribution and DNS service:
Systemd-resolved (Ubuntu 18.04+, Debian 10+):
sudo systemd-resolve --flush-caches
NSCD (Name Service Caching Daemon):
sudo systemctl restart nscd.service
Dnsmasq:
sudo systemctl restart dnsmasq
For frequent DNS changes during development, create scripts to automate flushing. Example Python script for cross-platform use:
import os
import platform
def flush_dns():
system = platform.system()
if system == "Windows":
os.system("ipconfig /flushdns")
elif system == "Darwin":
os.system("sudo dscacheutil -flushcache")
os.system("sudo killall -HUP mDNSResponder")
elif system == "Linux":
os.system("sudo systemd-resolve --flush-caches")
flush_dns()
After flushing, verify with these commands:
Windows:
ipconfig /displaydns | find "Records"
macOS/Linux:
dig example.com +stats
When you're debugging network issues or testing DNS changes during development, stale DNS records can cause frustrating delays. The DNS cache stores IP address mappings to speed up subsequent requests, but outdated entries may persist for their TTL (Time to Live) duration.
For Windows 10/11 and Windows Server:
# View current DNS cache
ipconfig /displaydns
# Flush DNS cache (admin rights required)
ipconfig /flushdns
To automate this in PowerShell scripts:
Clear-DnsClientCache
# Verify flush succeeded
if (-not (Get-DnsClientCache)) {
Write-Host "DNS cache successfully cleared"
}
Modern macOS versions use different caching mechanisms:
# For macOS Big Sur and later
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
# Alternative method that works on most versions
sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcaches
Create a handy bash alias in your .zshrc:
alias flushdns='sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder'
Linux handling varies by distribution and DNS service:
# Systemd-resolved (Ubuntu 18.04+)
sudo systemd-resolve --flush-caches
sudo systemd-resolve --statistics | grep Cache
# NSCD (Name Service Cache Daemon)
sudo systemctl restart nscd.service
# dnsmasq (common for local caching)
sudo systemctl restart dnsmasq
# For older systems without systemd
sudo /etc/init.d/nscd restart
After flushing, verify with these commands:
# Windows
nslookup example.com
# macOS/Linux
dig example.com +stats
Check the "Query time" - it should increase after flushing as the system performs fresh lookups.
When writing applications that depend on DNS:
// Python example with forced DNS refresh
import socket
def get_fresh_ip(hostname):
# Clear resolver cache
socket.gethostbyname.cache_clear()
return socket.gethostbyname(hostname)
For Node.js applications:
const dns = require('dns');
dns.setServers(['8.8.8.8']); // Force new DNS server
dns.resolve4('example.com', (err, addresses) => {
// Fresh lookup
});
If standard methods don't work:
- Check for multiple caching services running
- Verify system time is correct (TTL depends on timestamps)
- Try temporarily disabling IPv6
- Browser-specific caches may need clearing separately
# Chrome/Edge address bar:
chrome://net-internals/#dns