Conditional delete resource
Stay organized with collections
Save and categorize content based on your preferences.
Delete FHIR resources that match a search query.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code demonstrates how to conditionally delete FHIR resources using the Cloud Healthcare API.\u003c/p\u003e\n"],["\u003cp\u003eThe Go code sample shows how to delete resources based on a query, such as those updated within the last 48 hours.\u003c/p\u003e\n"],["\u003cp\u003eThe Python code sample illustrates deleting FHIR resources based on specific criteria, like all observations with a "cancelled" status.\u003c/p\u003e\n"],["\u003cp\u003eBoth samples require setting up authentication with Application Default Credentials and utilize the Cloud Healthcare API client libraries.\u003c/p\u003e\n"]]],[],null,["# Conditional delete resource\n\nDelete FHIR resources that match a search query.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Cloud Healthcare API quickstart using\nclient libraries](https://cloud.google.com/healthcare-api/docs/store-healthcare-data-client-library).\n\n\nFor more information, see the\n[Cloud Healthcare API Go API\nreference documentation](https://pkg.go.dev/google.golang.org/api/healthcare/v1).\n\n\nTo authenticate to Cloud Healthcare API, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \thealthcare \"google.golang.org/api/healthcare/v1beta1\"\n )\n\n // queryParamOpt is a googleapi.Option (https://godoc.org/google.golang.org/api/googleapi#CallOption)\n // that adds query parameters to an API call.\n type queryParamOpt struct {\n \tkey, value string\n }\n\n func (qp queryParamOpt) Get() (string, string) { return qp.key, qp.value }\n\n // ConditionalDeleteFHIRResource conditionally deletes an FHIR resource.\n func ConditionalDeleteFHIRResource(w io.Writer, projectID, location, datasetID, fhirStoreID, resourceType string) error {\n \t// projectID := \"my-project\"\n \t// location := \"us-central1\"\n \t// datasetID := \"my-dataset\"\n \t// fhirStoreID := \"my-fhir-store\"\n \t// resourceType := \"Patient\"\n\n \tctx := context.Background()\n\n \thealthcareService, err := healthcare.NewService(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"healthcare.NewService: %w\", err)\n \t}\n\n \tfhirService := healthcareService.Projects.Locations.Datasets.FhirStores.Fhir\n\n \tparent := fmt.Sprintf(\"projects/%s/locations/%s/datasets/%s/fhirStores/%s\", projectID, location, datasetID, fhirStoreID)\n\n \tcall := fhirService.ConditionalDelete(parent, resourceType)\n\n \t// Refine your search by appending tags to the request in the form of query\n \t// parameters. This searches for resources updated in the last 48 hours.\n \ttwoDaysAgo := time.Now().Add(-48 * time.Hour).Format(\"2006-01-02\")\n \tlastUpdated := queryParamOpt{key: \"_lastUpdated\", value: \"gt\" + twoDaysAgo}\n\n \tif _, err := call.Do(lastUpdated); err != nil {\n \t\treturn fmt.Errorf(\"ConditionalDelete: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Deleted %q\", parent)\n\n \treturn nil\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Cloud Healthcare API quickstart using\nclient libraries](https://cloud.google.com/healthcare-api/docs/store-healthcare-data-client-library).\n\n\nFor more information, see the\n[Cloud Healthcare API Python API\nreference documentation](https://googleapis.github.io/google-api-python-client/docs/dyn/healthcare_v1).\n\n\nTo authenticate to Cloud Healthcare API, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def conditional_delete_resource(\n service_account_json, base_url, project_id, cloud_region, dataset_id, fhir_store_id\n ):\n \"\"\"Deletes FHIR resources that match a search query.\"\"\"\n url = f\"{base_url}/projects/{project_id}/locations/{cloud_region}\"\n\n # The search query in this request deletes all Observations\n # with a status of 'cancelled'.\n resource_path = \"{}/datasets/{}/fhirStores/{}/fhir/Observation\".format(\n url, dataset_id, fhir_store_id\n )\n # The search query is passed in as a query string parameter.\n params = {\"status\": \"cancelled\"}\n\n # Make an authenticated API request\n session = get_session(service_account_json)\n\n response = session.delete(resource_path, params=params)\n print(response.url)\n if response.status_code != 404: # Don't consider missing to be error\n response.raise_for_status()\n\n print(\"Conditionally deleted all Observations with status='cancelled'.\")\n\n return response\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=healthcare)."]]