Show to clear the DNS cache with systemd
How to clear the DNS resolver cache with systemd?
To clear the systemd resolver cache use the resolvectl command with the 'flush-caches' subcommand.
resolvectl flush-cachesWhen using systemd, it is common to have the resolve daemon being responsible for handling DNS requests. It might be possible that a previous query is cached in this local resolver. To clear out this cache, we need the resolvectl in combination with the flush-caches subcommand.
Clear DNS cache
On modern systemd implementations, run the following command:
resolvectl flush-caches
Under normal conditions, this command won’t show any result.
If your Linux distribution is older, then use systemd-resolve --flush-caches
instead.
To confirm that the cache is empty, retrieve the statistics and look at the current cache size.
# resolvectl statistics
DNSSEC supported by current servers: no
Transactions
Current Transactions: 0
Total Transactions: 145185
Cache
Current Cache Size: 0
Cache Hits: 30468
Cache Misses: 46205
DNSSEC Verdicts
Secure: 0
Insecure: 0
Bogus: 0
Indeterminate: 0
In this case is the cache size back to zero, which is the expected result. The DNS cache is now empty and can be slowly filled again when new DNS requests are made.