Delete a context cache Stay organized with collections Save and categorize content based on your preferences.
To delete a context cache, you need its cache ID, the Google Cloud project ID withwhich the context cache is associated, and the region where the request tocreate the context cachewas processed. The cache ID of a context cache is returned when you create thecontext cache. You can also get the cache ID of each context cache associatedwith a project using thecontext cache list command.
Delete context cache example
The following example shows you how to delete a context cache.
Python
Install
pip install --upgrade google-genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
fromgoogleimportgenaiclient=genai.Client()# Delete content cache using name# E.g cache_name = 'projects/111111111111/locations/.../cachedContents/1111111111111111111'client.caches.delete(name=cache_name)print("Deleted Cache",cache_name)# Example response# Deleted Cache projects/111111111111/locations/.../cachedContents/1111111111111111111Go
Learn how to install or update theGo.
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
import("context""fmt""io"genai"google.golang.org/genai")//deleteContentCacheshowshowtodeletecontentcache.funcdeleteContentCache(wio.Writer,cacheNamestring)error{ctx:=context.Background()client,err:=genai.NewClient(ctx, &genai.ClientConfig{HTTPOptions:genai.HTTPOptions{APIVersion:"v1"},})iferr!=nil{returnfmt.Errorf("failed to create genai client: %w",err)}_,err=client.Caches.Delete(ctx,cacheName, &genai.DeleteCachedContentConfig{})iferr!=nil{returnfmt.Errorf("failed to delete content cache: %w",err)}fmt.Fprintf(w,"Deleted cache %q\n",cacheName)//Exampleresponse://Deletedcache"projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"returnnil}Java
Learn how to install or update theJava.
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
importcom.google.genai.Client;importcom.google.genai.types.HttpOptions;publicclassContentCacheDelete{publicstaticvoidmain(String[]args){//TODO(developer):Replacethesevariablesbeforerunningthesample.//E.gcacheName="projects/111111111111/locations/global/cachedContents/1111111111111111111"StringcacheName="your-cache-name";contentCacheDelete(cacheName);}//DeletesthecacheusingthespecifiedcachenamepublicstaticvoidcontentCacheDelete(StringcacheName){//Initializeclientthatwillbeusedtosendrequests.Thisclientonlyneedstobecreated//once,andcanbereusedformultiplerequests.try(Clientclient=Client.builder().location("global").vertexAI(true).httpOptions(HttpOptions.builder().apiVersion("v1").build()).build()){client.caches.delete(cacheName,null);System.out.println("Deleted cache: "+cacheName);//Exampleresponse//Deletedcache:projects/111111111111/locations/global/cachedContents/1111111111111111111}}}Node.js
Install
npm install @google/genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
const{GoogleGenAI}=require('@google/genai');constGOOGLE_CLOUD_PROJECT=process.env.GOOGLE_CLOUD_PROJECT;constGOOGLE_CLOUD_LOCATION=process.env.GOOGLE_CLOUD_LOCATION||'global';asyncfunctiondeleteContentCache(projectId=GOOGLE_CLOUD_PROJECT,location=GOOGLE_CLOUD_LOCATION,cacheName='example-cache'){constclient=newGoogleGenAI({vertexai:true,project:projectId,location:location,httpOptions:{apiVersion:'v1',},});console.log('Removing cache');constcontentCache=awaitclient.caches.delete({name:cacheName,});console.log(contentCache.text);returncontentCache;}//Exampleresponse//DeletedCacheprojects/111111111111/locations/us-central1/cachedContents/1111111111111111111REST
The following shows how to use REST to delete a context cache associated with a Google Cloud project by sending a DELETE request to the publisher model endpoint.
Before using any of the request data, make the following replacements:
- PROJECT_ID: Yourproject ID.
- LOCATION: The region where the request tocreate the context cache was processed and where the cached content is stored.
- CACHE_ID: The ID of the context cache to delete. The context cache ID is returned when you create the context cache. You can also find context cache IDs by listing the context caches for a Google Cloud project using. For more information, seecreate a context cache andlist context caches.
HTTP method and URL:
DELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID
To send your request, choose one of these options:
curl
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID"
PowerShell
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID" | Select-Object -Expand Content
If the delete operation succeeds, the response is empty:
Response
{ }Example curl command
LOCATION="us-central1"PROJECT_ID="PROJECT_ID"CACHE_ID="CACHE_ID"curl\-XDELETE\-H"Authorization: Bearer$(gcloudauthprint-access-token)"\https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/${CACHE_ID}What's next
- Learn how tocreate a new context cache.
- Learn how toget information about all context caches associated with a Google Cloud project.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.