# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=us-central1exportGOOGLE_GENAI_USE_VERTEXAI=True
fromgoogleimportgenaiclient=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
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=us-central1exportGOOGLE_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}
REST
以下示例展示了如何使用 REST 通过向发布方模型端点发送 DELETE 请求来删除与 Google Cloud 项目关联的上下文缓存。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-25。"],[],[],null,["# Delete a context cache\n\nTo delete a context cache, you need its cache ID, the Google Cloud project ID with\nwhich the context cache is associated, and the region where the request to\n[create the context cache](/vertex-ai/generative-ai/docs/context-cache/context-cache-create)\nwas processed. The cache ID of a context cache is returned when you create the\ncontext cache. You can also get the cache ID of each context cache associated\nwith a project using the\n[context cache list command](/vertex-ai/generative-ai/docs/context-cache/context-cache-getinfo#get-context-cache-list).\n\nDelete context cache example\n----------------------------\n\nThe following example shows you how to delete a context cache. \n\n### Python\n\n#### Install\n\n```\npip install --upgrade google-genai\n```\n\n\nTo learn more, see the\n[SDK reference documentation](https://googleapis.github.io/python-genai/).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=us-central1\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n from google import genai\n\n client = genai.Client()\n # Delete content cache using name\n # E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'\n client.caches.delete(name=cache_name)\n print(\"Deleted Cache\", cache_name)\n # Example response\n # Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111\n\n### Go\n\nLearn how to install or update the [Go](/vertex-ai/generative-ai/docs/sdks/overview).\n\n\nTo learn more, see the\n[SDK reference documentation](https://pkg.go.dev/google.golang.org/genai).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=us-central1\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tgenai \"google.golang.org/genai\"\n )\n\n // deleteContentCache shows how to delete content cache.\n func deleteContentCache(w io.Writer, cacheName string) error {\n \tctx := context.Background()\n\n \tclient, err := genai.NewClient(ctx, &genai.ClientConfig{\n \t\tHTTPOptions: genai.HTTPOptions{APIVersion: \"v1\"},\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create genai client: %w\", err)\n \t}\n\n \t_, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to delete content cache: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Deleted cache %q\\n\", cacheName)\n\n \t// Example response:\n \t// Deleted cache \"projects/111111111111/locations/us-central1/cachedContents/1111111111111111111\"\n\n \treturn nil\n }\n\n### REST\n\n\nThe following shows how to use REST to delete a context cache associated with\na Google Cloud project by sending a DELETE request to the publisher model endpoint.\n\n\nBefore using any of the request data,\nmake the following replacements:\n\n- \u003cvar class=\"edit\" scope=\"PROJECT_ID\" translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Your [project ID](/resource-manager/docs/creating-managing-projects#identifiers).\n- \u003cvar class=\"edit\" scope=\"LOCATION\" translate=\"no\"\u003eLOCATION\u003c/var\u003e: The region where the request to [create the context cache](/vertex-ai/generative-ai/docs/context-cache/context-cache-create) was processed and where the cached content is stored.\n- \u003cvar class=\"edit\" scope=\"CACHE_ID\" translate=\"no\"\u003eCACHE_ID\u003c/var\u003e: 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, see [create a context cache](/vertex-ai/generative-ai/docs/context-cache/context-cache-create) and [list context caches](/vertex-ai/generative-ai/docs/context-cache/context-cache-getinfo#get-context-cache-list).\n\n\nHTTP method and URL:\n\n```\nDELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID\n```\n\nTo send your request, choose one of these options: \n\n#### curl\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) , or by using [Cloud Shell](/shell/docs), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nExecute the following command:\n\n```\ncurl -X DELETE \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n \"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID\"\n```\n\n#### PowerShell\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nExecute the following command:\n\n```\n$cred = gcloud auth print-access-token\n$headers = @{ \"Authorization\" = \"Bearer $cred\" }\n\nInvoke-WebRequest `\n -Method DELETE `\n -Headers $headers `\n -Uri \"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents/CACHE_ID\" | Select-Object -Expand Content\n```\n\nIf the delete operation succeeds, the response is empty:\n\n#### Response\n\n```\n{ }\n```\n\n### Example curl command\n\n LOCATION=\"us-central1\"\n PROJECT_ID=\"\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\"\n CACHE_ID=\"\u003cvar translate=\"no\"\u003eCACHE_ID\u003c/var\u003e\"\n\n curl \\\n -X DELETE \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/${CACHE_ID}\n\n\u003cbr /\u003e\n\nWhat's next\n-----------\n\n- Learn how to [create a new context cache](/vertex-ai/generative-ai/docs/context-cache/context-cache-create).\n- Learn how to [get information about all context caches associated with a Google Cloud project](/vertex-ai/generative-ai/docs/context-cache/context-cache-getinfo)."]]