I have setup varnish and varnish is working okay with service varnish restart command but i want to restart varnish with php bin/magento cache:flush command.
Does anyone suggest me how i can do this .
As described on https://www.varnish-software.com/developers/tutorials/configuring-varnish-magento/#6-making-cache-purges-work, you should register the Varnish endpoints with Magento.
You can use the following command, assuming Varnish runs on localhost
:
bin/magento setup:config:set --http-cache-hosts=localhost
Once Magento is aware of how to connect to Varnish, you can run the following command to flush the Varnish cache along with other Magento caches:
bin/magento cache:flush
If you want to debug the cache invalidation process in Varnish, you can run the following command:
sudo varnishadm ban.list
This command will print the list of bans that were issued by Varnish. this banning mechanism is leveraged by Magento to remove objects from the cache.
Here's some example output:
sudo varnishadm ban.list
Present bans:
1603272627.622051 0 - obj.status != 0
1603270370.244746 3 C
The ban that is on the list was issued through the following ban expression:
obj.status != 0
Literally every HTTP status code of objects stored in the cache is not zero, so that expression would invalidate the entire cache.
In your case, the ban expression will be related to properties that were set by Magento.