Delete graph data with DML

Delete Spanner Graph data using DML.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

To learn how to install and use the client library for Spanner, see Spanner client libraries.

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

voidDeleteDataWithDml(google::cloud::spanner::Clientclient){
using::google::cloud::StatusOr;
namespacespanner=::google::cloud::spanner;
autocommit_result=client.Commit([&client](spanner::Transactiontxn)
->StatusOr<spanner::Mutations>{
autodeleted=client.ExecuteDml(
std::move(txn),
spanner::SqlStatement(
"DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2"));
if(!deleted)returnstd::move(deleted).status();
returnspanner::Mutations{};
});
if(!commit_result)throwstd::move(commit_result).status();
commit_result=client.Commit(
[&client](spanner::Transactiontxn)->StatusOr<spanner::Mutations>{
autodeleted=client.ExecuteDml(
std::move(txn),
spanner::SqlStatement("DELETE FROM Account WHERE id = 2"));
if(!deleted)returnstd::move(deleted).status();
returnspanner::Mutations{};
});
if(!commit_result)throwstd::move(commit_result).status();
std::cout << "Delete was successful [spanner_delete_graph_data_with_dml]\n";
}

Go

To learn how to install and use the client library for Spanner, see Spanner client libraries.

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import(
"context"
"fmt"
"io"
"cloud.google.com/go/spanner"
)
funcdeleteGraphDataWithDml(wio.Writer,dbstring)error{
ctx:=context.Background()
client,err:=spanner.NewClient(ctx,db)
iferr!=nil{
returnerr
}
deferclient.Close()
// Execute a ReadWriteTransaction to update the 'AccountTransferAccount'
// table underpinning 'AccountTransferAccount' edges in 'FinGraph'. The
// function run by ReadWriteTransaction executes an 'DELETE' SQL DML
// statement. This has the effect of deleting the 'AccountTransferAccount'
// edge where the source 'id' is 1 and the destination 'id' is 2 from the graph.
_,err1:=client.ReadWriteTransaction(ctx,func(ctxcontext.Context,txn*spanner.ReadWriteTransaction)error{
stmt:=spanner.Statement {SQL:`DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2`}
rowCount,err:=txn.Update(ctx,stmt)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"%d AccountTransferAccount record(s) deleted.\n",rowCount)
returnnil
})
iferr1!=nil{
returnerr1
}
// Execute a ReadWriteTransaction to update the 'Account' table underpinning
//'Account' nodes in 'FinGraph'. In 'FinGraph', nodes can only be deleted
// after any edges referencing the nodes have been deleted first. The function
// run by ReadWriteTransaction executes an 'DELETE' SQL DML statement. This has
// the effect of deleting the 'Account' node whose 'id' is 1 from the graph.
_,err2:=client.ReadWriteTransaction(ctx,func(ctxcontext.Context,txn*spanner.ReadWriteTransaction)error{
stmt:=spanner.Statement {SQL:`DELETE FROM Account WHERE id = 2`}
rowCount,err:=txn.Update(ctx,stmt)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"%d Account record(s) deleted.\n",rowCount)
returnnil
})
returnerr2
}

Java

To learn how to install and use the client library for Spanner, see Spanner client libraries.

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

staticvoiddeleteUsingDml(DatabaseClientdbClient){
dbClient
.readWriteTransaction()
.run(
transaction->{
Stringsql="DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2";
longrowCount=transaction.executeUpdate(Statement.of(sql));
System.out.printf("%d AccountTransferAccount record(s) deleted.\n",rowCount);
returnnull;
});
dbClient
.readWriteTransaction()
.run(
transaction->{
Stringsql="DELETE FROM Account WHERE id = 2";
longrowCount=transaction.executeUpdate(Statement.of(sql));
System.out.printf("%d Account record(s) deleted.\n",rowCount);
returnnull;
});
}

Python

To learn how to install and use the client library for Spanner, see Spanner client libraries.

To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

defdelete_data_with_dml(instance_id, database_id):
"""Deletes sample data from the database using a DML statement."""
 spanner_client = spanner.Client()
 instance = spanner_client.instance(instance_id)
 database = instance.database(database_id)
 defdelete_transfers(transaction):
 row_ct = transaction.execute_update(
 "DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2"
 )
 print("{} AccountTransferAccount record(s) deleted.".format(row_ct))
 defdelete_accounts(transaction):
 row_ct = transaction.execute_update("DELETE FROM Account WHERE id = 2")
 print("{} Account record(s) deleted.".format(row_ct))
 database.run_in_transaction(delete_transfers)
 database.run_in_transaction(delete_accounts)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

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.