This page explains how to bulk-delete FHIR resources from a FHIR store using a long-running operation.
You can delete multiple FHIR resources in a single operation based on filters like resource type and last updated time, or by providing a list of specific resource IDs in a Cloud Storage file. This is useful for data lifecycle management and cost savings.
Before you begin
Before you can bulk-delete FHIR resources, ensure the following:
- You must have the
healthcare.fhirStores.bulkDeletepermission on the FHIR store. - If you specify a
gcsDestination, the Cloud Healthcare Service Agent service account must have theroles/storage.objectAdminrole on the destination bucket. For more information, see FHIR store Cloud Storage permissions. - If you specify a
gcsSource, the Cloud Healthcare Service Agent service account must have theroles/storage.objectViewerrole on the source bucket. For more information, see Bulk-deleting FHIR resources.
Bulk-deleting FHIR resources
To bulk-delete FHIR resources, use the projects.locations.datasets.fhirStores.bulkDelete method.
This method returns a long-running operation (LRO). You can track the status of the LRO using the operation name returned by the API call.
The following sample shows how to make a POST request to bulk-delete all Observation and Encounter resources in a FHIR store that were last updated before a specific timestamp.
REST
Before using any of the request data, make the following replacements:
PROJECT_ID: the ID of your Google Cloud projectLOCATION: the dataset locationDATASET_ID: the FHIR store's parent datasetFHIR_STORE_ID: the FHIR store ID
Request JSON body:
{
"type": "Observation,Encounter",
"versionConfig": "ALL",
"until": "2025-01-01T00:00:00Z",
"gcsDestination": {
"uriPrefix": "gs://BUCKET/DIRECTORY"
}
}
To send your request, choose one of these options:
curl
Save the request body in a file named request.json.
Run the following command in the terminal to create or overwrite
this file in the current directory:
cat > request.json << 'EOF'
{
"type": "Observation,Encounter",
"versionConfig": "ALL",
"until": "2025-01-01T00:00:00Z",
"gcsDestination": {
"uriPrefix": "gs://BUCKET/DIRECTORY"
}
}
EOFThen execute the following command to send your REST request:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d @request.json \
"https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID:bulkDelete"
PowerShell
Save the request body in a file named request.json.
Run the following command in the terminal to create or overwrite
this file in the current directory:
@'
{
"type": "Observation,Encounter",
"versionConfig": "ALL",
"until": "2025-01-01T00:00:00Z",
"gcsDestination": {
"uriPrefix": "gs://BUCKET/DIRECTORY"
}
}
'@ | Out-File -FilePath request.json -Encoding utf8Then execute the following command to send your REST request:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json" `
-InFile request.json `
-Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID:bulkDelete" | Select-Object -Expand Content
Bulk-deleting FHIR resources by resource ID
To delete a specific list of FHIR resources, set the gcsSource.uri field to the Cloud Storage location of one or more files that contain the resource IDs to delete.
The input files must meet the following requirements:
- Each line contains one resource in the format
RESOURCE_TYPE/RESOURCE_ID, for examplePatient/8af1cf10-0fec-4bb3-bde1-77f56a3d8281. Raw resource IDs without a resource type aren't supported. - The URI must point to a single file, such as
gs://BUCKET/ids.txt, or a wildcard pattern, such asgs://BUCKET/DIRECTORY/*.ndjson. The URI can't be a bucket or a directory that ends in/.
The following content is an example of a valid input file:
Patient/8af1cf10-0fec-4bb3-bde1-77f56a3d8281 Observation/2c9d4a3e-6b1f-4e2a-9f0d-1a2b3c4d5e6f Encounter/7f3e2d1c-0b9a-4c8d-8e7f-6a5b4c3d2e1f
Note the following behavior:
- The
gcsSourcefield can't be used together with thetypeoruntilfilters. If you specifygcsSourcetogether with either filter, the request fails. - You can use
gcsSourcewithversionConfigandgcsDestination. - Blank lines are ignored. Lines that don't match the
RESOURCE_TYPE/RESOURCE_IDformat or that contain an unsupported resource type are skipped, and the operation continues. - Resource IDs that don't exist in the FHIR store are skipped.
- Don't modify or delete the input files until the operation completes.
The following sample shows how to make a POST request to bulk-delete the FHIR resources listed in a Cloud Storage file.
REST
Before using any of the request data, make the following replacements:
PROJECT_ID: the ID of your Google Cloud projectLOCATION: the dataset locationDATASET_ID: the FHIR store's parent datasetFHIR_STORE_ID: the FHIR store IDBUCKET/PATH_TO_IDS_FILE: the Cloud Storage location of the file, or wildcard pattern, that lists the resources to delete in the formatRESOURCE_TYPE/RESOURCE_ID
Request JSON body:
{
"versionConfig": "ALL",
"gcsSource": {
"uri": "gs://BUCKET/PATH_TO_IDS_FILE"
}
}
To send your request, choose one of these options:
curl
Save the request body in a file named request.json.
Run the following command in the terminal to create or overwrite
this file in the current directory:
cat > request.json << 'EOF'
{
"versionConfig": "ALL",
"gcsSource": {
"uri": "gs://BUCKET/PATH_TO_IDS_FILE"
}
}
EOFThen execute the following command to send your REST request:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d @request.json \
"https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID:bulkDelete"
PowerShell
Save the request body in a file named request.json.
Run the following command in the terminal to create or overwrite
this file in the current directory:
@'
{
"versionConfig": "ALL",
"gcsSource": {
"uri": "gs://BUCKET/PATH_TO_IDS_FILE"
}
}
'@ | Out-File -FilePath request.json -Encoding utf8Then execute the following command to send your REST request:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json" `
-InFile request.json `
-Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID:bulkDelete" | Select-Object -Expand Content
Viewing the LRO status
To view the status of the bulk-delete operation, use the operations.get method with the operation name returned from the bulkDelete call.
REST
Before using any of the request data, make the following replacements:
PROJECT_ID: the ID of your Google Cloud projectLOCATION: the dataset locationDATASET_ID: the FHIR store's parent datasetOPERATION_ID: the ID returned from the long-running operation
To send your request, choose one of these options:
curl
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://healthcare.googleapis.com/v1alpha2/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/operations/OPERATION_ID"
PowerShell
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://healthcare.googleapis.com/v1alpha2/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/operations/OPERATION_ID" | Select-Object -Expand Content
Tracking deleted resources
When the bulk-delete operation completes, if a gcsDestination was specified, a summary file is generated in Cloud Storage. This file contains a list of the resource IDs for the current version resources that were deleted during the operation.
Limitations
Referential integrity is not guaranteed during a bulk-delete operation. It is recommended to use this operation only when referential integrity is not required or is known to be satisfied after the operation.