Mulai 29 April 2025, model Gemini 1.5 Pro dan Gemini 1.5 Flash tidak tersedia di project yang belum pernah menggunakan model ini, termasuk project baru. Untuk mengetahui detailnya, lihat Versi dan siklus proses model.
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Panduan ini menunjukkan cara menghapus cache konteks menggunakan Vertex AI SDK untuk Python atau REST API.
Sebelum memulai
Anda dapat menghapus cache konteks menggunakan Vertex AI SDK untuk Python atau REST API. Tabel berikut membantu Anda memilih metode yang paling sesuai dengan kasus penggunaan Anda.
Metode
Deskripsi
Kasus Penggunaan
Vertex AI SDK untuk Python
Gunakan library google-cloud-aiplatform untuk berinteraksi dengan API dari kode Python Anda.
Direkomendasikan untuk developer yang membangun aplikasi di Python, karena menyederhanakan autentikasi dan interaksi API.
REST API
Kirim permintaan DELETE HTTP langsung ke endpoint Vertex AI API.
Berguna untuk membuat skrip dalam berbagai bahasa, untuk pengujian cepat dengan alat seperti curl, atau di lingkungan tempat penginstalan library klien tidak memungkinkan.
Untuk menghapus context cache, Anda memerlukan informasi berikut:
Tetapkan variabel lingkungan untuk menggunakan Gen AI SDK dengan 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=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
Tetapkan variabel lingkungan untuk menggunakan Gen AI SDK dengan 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=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
Untuk menghapus cache konteks, kirim permintaan DELETE ke endpoint Vertex AI API.
Sebelum menggunakan salah satu data permintaan,
lakukan penggantian berikut:
LOCATION: Region tempat permintaan untuk
membuat cache konteks
diproses dan tempat konten yang di-cache disimpan.
CACHE_ID: ID cache konteks yang akan dihapus. ID context cache ditampilkan saat Anda
membuat context cache. Anda juga dapat menemukan ID context cache dengan mencantumkan context cache untuk project menggunakan Google Cloud . Untuk mengetahui informasi selengkapnya, lihat
membuat context cache dan
mencantumkan context cache.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]