Python/Selenium - Clear the cache and cookies in my chrome webdriver?

Taken from this post:

For cookies, you can use the delete_all_cookies function:

driver.delete_all_cookies()

For cache, there isn't a direct way to do this through Selenium. If you are trying to make sure everything is cleared at the beginning of starting a Chrome driver, or when you are done, then you don't need to do anything. Every time you initialize a webdriver, it is a brand new instance with no cache, cookies, or history. Every time you terminate the driver, all these are cleared.


Cache clearing for Chromedriver with Selenium in November 2020:

Use this function which opens a new tab, choses to delete everything, confirms and goes back to previously active tab.

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("path/to/chromedriver.exe")

def delete_cache():
    driver.execute_script("window.open('');")
    time.sleep(2)
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(2)
    driver.get('chrome://settings/clearBrowserData') # for old chromedriver versions use cleardriverData
    time.sleep(2)
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 3 + Keys.DOWN * 3) # send right combination
    actions.perform()
    time.sleep(2)
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 4 + Keys.ENTER) # confirm
    actions.perform()
    time.sleep(5) # wait some time to finish
    driver.close() # close this tab
    driver.switch_to.window(driver.window_handles[0]) # switch back
delete_cache()

UPDATE 01/2021: Apparently the settings section in chromedriver is subject to change. The old version was chrome://settings/cleardriverData. In any doubt, go to chrome://settings/, click on the browser data/cache clearing section and copy the new term.