Delete context cache

Delete a specific context cache that's no longer used.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Go API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 genai 
  
 "google.golang.org/genai" 
 ) 
 // deleteContentCache shows how to delete content cache. 
 func 
  
 deleteContentCache 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 cacheName 
  
 string 
 ) 
  
 error 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 genai 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
& genai 
 . 
 ClientConfig 
 { 
  
 HTTPOptions 
 : 
  
 genai 
 . 
 HTTPOptions 
 { 
 APIVersion 
 : 
  
 "v1" 
 }, 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to create genai client: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 _ 
 , 
  
 err 
  
 = 
  
 client 
 . 
 Caches 
 . 
 Delete 
 ( 
 ctx 
 , 
  
 cacheName 
 , 
  
& genai 
 . 
 DeleteCachedContentConfig 
 {}) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to delete content cache: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Deleted cache %q\n" 
 , 
  
 cacheName 
 ) 
  
 // Example response: 
  
 // Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Java API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.genai.Client 
 ; 
 import 
  
 com.google.genai.types.HttpOptions 
 ; 
 public 
  
 class 
 ContentCacheDelete 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // E.g cacheName = "projects/111111111111/locations/global/cachedContents/1111111111111111111" 
  
 String 
  
 cacheName 
  
 = 
  
 "your-cache-name" 
 ; 
  
 contentCacheDelete 
 ( 
 cacheName 
 ); 
  
 } 
  
 // Deletes the cache using the specified cache name 
  
 public 
  
 static 
  
 void 
  
 contentCacheDelete 
 ( 
 String 
  
 cacheName 
 ) 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
 Client 
  
 client 
  
 = 
  
 Client 
 . 
 builder 
 () 
  
 . 
 location 
 ( 
 "global" 
 ) 
  
 . 
 vertexAI 
 ( 
 true 
 ) 
  
 . 
 httpOptions 
 ( 
 HttpOptions 
 . 
 builder 
 (). 
 apiVersion 
 ( 
 "v1" 
 ). 
 build 
 ()) 
  
 . 
 build 
 ()) 
  
 { 
  
 client 
 . 
 caches 
 . 
 delete 
 ( 
 cacheName 
 , 
  
 null 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Deleted cache: " 
  
 + 
  
 cacheName 
 ); 
  
 // Example response 
  
 // Deleted cache: projects/111111111111/locations/global/cachedContents/1111111111111111111 
  
 } 
  
 } 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Python API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google 
  
 import 
 genai 
 client 
 = 
 genai 
 . 
 Client 
 () 
 # Delete content cache using name 
 # E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111' 
 client 
 . 
 caches 
 . 
 delete 
 ( 
 name 
 = 
 cache_name 
 ) 
 print 
 ( 
 "Deleted Cache" 
 , 
 cache_name 
 ) 
 # Example response 
 #   Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: