Bulk delete with partitioned DML
Stay organized with collections
Save and categorize content based on your preferences.
Bulk delete data by using a partitioned DML statement.
Explore further
For detailed documentation that includes this code sample, see the following:
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"]],[],[],[],null,["Bulk delete data by using a partitioned DML statement.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Insert, update, and delete data using data manipulation language (DML)](/spanner/docs/dml-tasks)\n- [Partitioned Data Manipulation Language](/spanner/docs/dml-partitioned)\n- [Transactions overview](/spanner/docs/transactions)\n\nCode sample \n\nC++\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 void DmlPartitionedDelete(google::cloud::spanner::Client client) {\n namespace spanner = ::google::cloud::spanner;\n auto result = client.ExecutePartitionedDml(\n spanner::SqlStatement(\"DELETE FROM Singers WHERE SingerId \u003e 10\"));\n if (!result) throw std::move(result).status();\n std::cout \u003c\u003c \"Deleted at least \" \u003c\u003c result-\u003erow_count_lower_bound\n \u003c\u003c \" row(s) [spanner_dml_partitioned_delete]\\n\";\n }\n\nC#\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.html;\n using System;\n using System.Threading.Tasks;\n\n public class DeleteUsingPartitionedDmlCoreAsyncSample\n {\n public async Task\u003clong\u003e DeleteUsingPartitionedDmlCoreAsync(string projectId, string instanceId, string databaseId)\n {\n string connectionString = $\"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}\";\n\n using var connection = new https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html(connectionString);\n await connection.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html#Google_Cloud_Spanner_Data_SpannerConnection_OpenAsync_Google_Cloud_Spanner_Data_SpannerTransactionCreationOptions_Google_Cloud_Spanner_Data_SpannerTransactionOptions_System_Threading_CancellationToken_();\n\n using var cmd = connection.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html#Google_Cloud_Spanner_Data_SpannerConnection_CreateDmlCommand_System_String_Google_Cloud_Spanner_Data_SpannerParameterCollection_(\"DELETE FROM Singers WHERE SingerId \u003e 10\");\n long rowCount = await cmd.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerCommand.html#Google_Cloud_Spanner_Data_SpannerCommand_ExecutePartitionedUpdateAsync_System_Threading_CancellationToken_();\n\n Console.WriteLine($\"{rowCount} row(s) deleted...\");\n return rowCount;\n }\n }\n\nGo\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/spanner\"\n )\n\n func deleteUsingPartitionedDML(w io.Writer, db string) error {\n \tctx := context.Background()\n \tclient, err := spanner.NewClient(ctx, db)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer client.Close()\n\n \tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{SQL: \"DELETE FROM Singers WHERE SingerId \u003e 10\"}\n \trowCount, err := client.PartitionedUpdate(ctx, stmt)\n \tif err != nil {\n \t\treturn err\n\n \t}\n \tfmt.Fprintf(w, \"%d record(s) deleted.\", rowCount)\n \treturn nil\n }\n\nJava\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 static void deleteUsingPartitionedDml(DatabaseClient dbClient) {\n String sql = \"DELETE FROM Singers WHERE SingerId \u003e 10\";\n long rowCount = dbClient.executePartitionedUpdate(Statement.of(sql));\n System.out.printf(\"%d records deleted.\\n\", rowCount);\n }\n\nNode.js\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 // Imports the Google Cloud client library\n const {Spanner} = require('https://cloud.google.com/nodejs/docs/reference/spanner/latest/overview.html');\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const projectId = 'my-project-id';\n // const instanceId = 'my-instance';\n // const databaseId = 'my-database';\n\n // Creates a client\n const spanner = new https://cloud.google.com/nodejs/docs/reference/spanner/latest/spanner/spanner.html({\n projectId: projectId,\n });\n\n // Gets a reference to a Cloud Spanner instance and database\n const instance = spanner.instance(instanceId);\n const database = instance.database(databaseId);\n\n try {\n const [rowCount] = await database.https://cloud.google.com/nodejs/docs/reference/spanner/latest/spanner/database.html({\n sql: 'DELETE FROM Singers WHERE SingerId \u003e 10',\n });\n console.log(`Successfully deleted ${rowCount} records.`);\n } catch (err) {\n console.error('ERROR:', err);\n } finally {\n // Close the database when finished.\n database.close();\n }\n\nPHP\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 use Google\\Cloud\\Spanner\\SpannerClient;\n\n /**\n * Delete sample data in the database by partition with a DML statement.\n *\n * This updates the `MarketingBudget` column which must be created before\n * running this sample. You can add the column by running the `add_column`\n * sample or by running this DDL statement against your database:\n *\n * ALTER TABLE Albums ADD COLUMN MarketingBudget INT64\n *\n * Example:\n * ```\n * update_data($instanceId, $databaseId);\n * ```\n *\n * @param string $instanceId The Spanner instance ID.\n * @param string $databaseId The Spanner database ID.\n */\n function delete_data_with_partitioned_dml(string $instanceId, string $databaseId): void\n {\n $spanner = new SpannerClient();\n $instance = $spanner-\u003einstance($instanceId);\n $database = $instance-\u003edatabase($databaseId);\n\n $rowCount = $database-\u003eexecutePartitionedUpdate(\n 'DELETE FROM Singers WHERE SingerId \u003e 10'\n );\n\n printf('Deleted %d row(s).' . PHP_EOL, $rowCount);\n }\n\nPython\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 # instance_id = \"your-spanner-instance\"\n # database_id = \"your-spanner-db-id\"\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n row_ct = database.execute_partitioned_dml(\"DELETE FROM Singers WHERE SingerId \u003e 10\")\n\n print(\"{} record(s) deleted.\".format(row_ct))\n\nRuby\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, 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 # project_id = \"Your Google Cloud project ID\"\n # instance_id = \"Your Spanner instance ID\"\n # database_id = \"Your Spanner database ID\"\n\n require \"google/cloud/spanner\"\n\n spanner = Google::Cloud::https://cloud.google.com/ruby/docs/reference/activerecord-spanner-adapter/latest/Google-Cloud-Spanner.html.new project: project_id\n client = spanner.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project.html instance_id, database_id\n\n row_count = client.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client.html(\n \"DELETE FROM Singers WHERE SingerId \u003e 10\"\n )\n\n puts \"#{row_count} records deleted.\"\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=spanner)."]]