Delete data in a graph
Stay organized with collections
Save and categorize content based on your preferences.
Delete data in a Spanner Graph.
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,["# Delete data in a graph\n\nDelete data in a Spanner Graph.\n\nCode sample\n-----------\n\n### C++\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 DeleteData(google::cloud::spanner::Client client) {\n namespace spanner = ::google::cloud::spanner;\n\n // Delete the 'Owns' relationships with key (1,7) and (2,20).\n auto delete_ownerships =\n spanner::DeleteMutationBuilder(\"PersonOwnAccount\",\n spanner::KeySet()\n .AddKey(spanner::MakeKey(1, 7))\n .AddKey(spanner::MakeKey(2, 20)))\n .Build();\n\n // Delete transfers using the keys in the range [1, 8]\n auto delete_transfer_range =\n spanner::DeleteMutationBuilder(\n \"AccountTransferAccount\",\n spanner::KeySet().AddRange(spanner::MakeKeyBoundClosed(1),\n spanner::MakeKeyBoundOpen(8)))\n .Build();\n\n // Deletes rows from the Account table and the AccountTransferAccount\n // table, because the AccountTransferAccount table is defined with\n // ON DELETE CASCADE.\n auto delete_accounts_all =\n spanner::MakeDeleteMutation(\"Account\", spanner::KeySet::All());\n\n // Deletes rows from the Person table and the PersonOwnAccount table,\n // because the PersonOwnAccount table is defined with ON DELETE CASCADE.\n auto delete_persons_all =\n spanner::MakeDeleteMutation(\"Person\", spanner::KeySet::All());\n\n auto commit_result = client.Commit(\n spanner::Mutations{delete_ownerships, delete_transfer_range,\n delete_accounts_all, delete_persons_all});\n if (!commit_result) throw std::move(commit_result).status();\n std::cout \u003c\u003c \"Delete was successful [spanner_delete_graph_data]\\n\";\n }\n\n### Go\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\"io\"\n\n \t\"cloud.google.com/go/spanner\"\n )\n\n func deleteGraphData(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 \t// Apply a series of mutations to tables underpinning edges and nodes in the\n \t// example graph. If there are referential integrity constraints defined\n \t// between edges and the nodes they connect, the edge must be deleted\n \t// before the nodes that the edge connects are deleted.\n \tm := []*spanner.Mutation{\n \t\t// spanner.Key can be used to delete a specific set of rows.\n \t\t// Delete the PersonOwnAccount rows with the key values (1,7) and (2,20).\n \t\tspanner.Delete(\"PersonOwnAccount\", spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Key{1, 7}),\n \t\tspanner.Delete(\"PersonOwnAccount\", spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Key{2, 20}),\n\n \t\t// spanner.KeyRange can be used to delete rows with a key in a specific range.\n \t\t// Delete a range of rows where the key prefix is \u003e=1 and \u003c8\n \t\tspanner.Delete(\"AccountTransferAccount\",\n \t\t\tspanner.KeyRange{Start: spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Key{1}, End: spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Key{8}, Kind: spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_ClosedOpen_ClosedClosed_OpenClosed_OpenOpen}),\n\n \t\t// spanner.AllKeys can be used to delete all the rows in a table.\n \t\t// Delete all Account rows, which will also delete the remaining\n \t\t// AccountTransferAccount rows since it was defined with ON DELETE CASCADE.\n \t\tspanner.Delete(\"Account\", spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_KeySet_AllKeys()),\n\n \t\t// Delete remaining Person rows, which will also delete the remaining\n \t\t// PersonOwnAccount rows since it was defined with ON DELETE CASCADE.\n \t\tspanner.Delete(\"Person\", spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_KeySet_AllKeys()),\n \t}\n \t_, err = client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Client_Apply(ctx, m)\n \treturn err\n }\n\n### Java\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 deleteData(DatabaseClient dbClient) {\n List\u003cMutation\u003e mutations = new ArrayList\u003c\u003e();\n\n // KeySet.Builder can be used to delete a specific set of rows.\n // Delete the PersonOwnAccount rows with the key values (1,7) and (2,20).\n mutations.add(\n Mutation.delete(\n \"PersonOwnAccount\",\n KeySet.newBuilder().addKey(Key.of(1, 7)).addKey(Key.of(2, 20)).build()));\n\n // KeyRange can be used to delete rows with a key in a specific range.\n // Delete a range of rows where the key prefix is \u003e=1 and \u003c8\n mutations.add(\n Mutation.delete(\n \"AccountTransferAccount\", KeySet.range(KeyRange.closedOpen(Key.of(1), Key.of(8)))));\n\n // KeySet.all() can be used to delete all the rows in a table.\n // Delete all Account rows, which will also delete the remaining\n // AccountTransferAccount rows since it was defined with ON DELETE CASCADE.\n mutations.add(Mutation.delete(\"Account\", KeySet.all()));\n\n // KeySet.all() can be used to delete all the rows in a table.\n // Delete all Person rows, which will also delete the remaining\n // PersonOwnAccount rows since it was defined with ON DELETE CASCADE.\n mutations.add(Mutation.delete(\"Person\", KeySet.all()));\n\n dbClient.write(mutations);\n System.out.printf(\"Records deleted.\\n\");\n }\n\n### Python\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 def delete_data(instance_id, database_id):\n \"\"\"Deletes sample data from the given database.\n\n The database, table, and data must already exist and can be created using\n `create_database` and `insert_data`.\n \"\"\"\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n # Delete individual rows\n ownerships_to_delete = spanner.KeySet(keys=[[1, 7], [2, 20]])\n\n # Delete a range of rows where the column key is \u003e=1 and \u003c8\n transfers_range = spanner.KeyRange(start_closed=[1], end_open=[8])\n transfers_to_delete = spanner.KeySet(ranges=[transfers_range])\n\n # Delete Account/Person rows, which will also delete the remaining\n # AccountTransferAccount and PersonOwnAccount rows because\n # AccountTransferAccount and PersonOwnAccount are defined with\n # ON DELETE CASCADE\n remaining_nodes = spanner.KeySet(all_=True)\n\n with database.batch() as batch:\n batch.delete(\"PersonOwnAccount\", ownerships_to_delete)\n batch.delete(\"AccountTransferAccount\", transfers_to_delete)\n batch.delete(\"Account\", remaining_nodes)\n batch.delete(\"Person\", remaining_nodes)\n\n print(\"Deleted data.\")\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=spanner)."]]