Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In dieser Anleitung erfahren Sie, wie Sie einen Kontextcache mit dem Vertex AI SDK für Python oder der REST API löschen.
Hinweise
Sie können einen Kontext-Cache mit dem Vertex AI SDK für Python oder der REST API löschen. Die folgende Tabelle hilft Ihnen bei der Auswahl der Methode, die am besten zu Ihrem Anwendungsfall passt.
Methode
Beschreibung
Anwendungsfall
Vertex AI SDK für Python
Verwenden Sie die google-cloud-aiplatform-Bibliothek, um über Ihren Python-Code mit der API zu interagieren.
Empfohlen für Entwickler, die Anwendungen in Python erstellen, da es die Authentifizierung und API-Interaktionen vereinfacht.
REST API
Senden Sie eine direkte HTTP-DELETE-Anfrage an den Vertex AI API-Endpunkt.
Nützlich für das Scripting in verschiedenen Sprachen, für schnelle Tests mit Tools wie curl oder in Umgebungen, in denen die Installation einer Clientbibliothek nicht möglich ist.
Wenn Sie einen Kontext-Cache löschen möchten, benötigen Sie die folgenden Informationen:
Umgebungsvariablen für die Verwendung des Gen AI SDK mit Vertex AI festlegen:
# 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
Umgebungsvariablen für die Verwendung des Gen AI SDK mit Vertex AI festlegen:
# 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
Wenn Sie einen Kontext-Cache löschen möchten, senden Sie eine DELETE-Anfrage an den Vertex AI API-Endpunkt.
Ersetzen Sie diese Werte in den folgenden Anfragedaten:
LOCATION: Die Region, in der die Anfrage zum Erstellen des Kontext-Cache verarbeitet wurde und in der der Cache-Inhalt gespeichert ist.
CACHE_ID: Die ID des zu löschenden Kontext-Cache. Die Kontext-Cache-ID wird zurückgegeben, wenn Sie den Kontext-Cache erstellen. Sie können Kontext-Cache-IDs auch finden, indem Sie die Kontext-Caches für ein Google Cloud -Projekt auflisten. Weitere Informationen finden Sie unter Kontext-Cache erstellen und Kontext-Caches auflisten.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-25 (UTC)."],[],[],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)."]]