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:
- Insert, update, and delete data using data manipulation language (DML)
- Partitioned Data Manipulation Language
- Transactions overview
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.
voidDmlPartitionedDelete(google::cloud::spanner::Clientclient){
namespacespanner=::google::cloud::spanner;
autoresult=client.ExecutePartitionedDml(
spanner::SqlStatement("DELETE FROM Singers WHERE SingerId > 10"));
if(!result)throwstd::move(result).status();
std::cout << "Deleted at least " << result->row_count_lower_bound
<< " row(s) [spanner_dml_partitioned_delete]\n";
}
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.
usingGoogle.Cloud.Spanner.Data ;
usingSystem;
usingSystem.Threading.Tasks;
publicclassDeleteUsingPartitionedDmlCoreAsyncSample
{
publicasyncTask<long>DeleteUsingPartitionedDmlCoreAsync(stringprojectId,stringinstanceId,stringdatabaseId)
{
stringconnectionString=$"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
usingvarconnection=newSpannerConnection (connectionString);
awaitconnection.OpenAsync ();
usingvarcmd=connection.CreateDmlCommand ("DELETE FROM Singers WHERE SingerId > 10");
longrowCount=awaitcmd.ExecutePartitionedUpdateAsync ();
Console.WriteLine($"{rowCount} row(s) deleted...");
returnrowCount;
}
}
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"
)
funcdeleteUsingPartitionedDML(wio.Writer,dbstring)error{
ctx:=context.Background()
client,err:=spanner.NewClient(ctx,db)
iferr!=nil{
returnerr
}
deferclient.Close()
stmt:=spanner.Statement {SQL:"DELETE FROM Singers WHERE SingerId > 10"}
rowCount,err:=client.PartitionedUpdate(ctx,stmt)
iferr!=nil{
returnerr
}
fmt.Fprintf(w,"%d record(s) deleted.",rowCount)
returnnil
}
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.
staticvoiddeleteUsingPartitionedDml(DatabaseClientdbClient){
Stringsql="DELETE FROM Singers WHERE SingerId > 10";
longrowCount=dbClient.executePartitionedUpdate(Statement.of(sql));
System.out.printf("%d records deleted.\n",rowCount);
}
Node.js
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.
// Imports the Google Cloud client library
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// Creates a client
constspanner=newSpanner ({
projectId:projectId,
});
// Gets a reference to a Cloud Spanner instance and database
constinstance=spanner.instance(instanceId);
constdatabase=instance.database(databaseId);
try{
const[rowCount]=awaitdatabase.runPartitionedUpdate ({
sql:'DELETE FROM Singers WHERE SingerId > 10',
});
console.log(`Successfully deleted ${rowCount} records.`);
}catch(err){
console.error('ERROR:',err);
}finally{
// Close the database when finished.
database.close();
}
PHP
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.
use Google\Cloud\Spanner\SpannerClient;
/**
* Delete sample data in the database by partition with a DML statement.
*
* This updates the `MarketingBudget` column which must be created before
* running this sample. You can add the column by running the `add_column`
* sample or by running this DDL statement against your database:
*
* ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
*
* Example:
* ```
* update_data($instanceId, $databaseId);
* ```
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function delete_data_with_partitioned_dml(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);
$rowCount = $database->executePartitionedUpdate(
'DELETE FROM Singers WHERE SingerId > 10'
);
printf('Deleted %d row(s).' . PHP_EOL, $rowCount);
}
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.
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
row_ct = database.execute_partitioned_dml("DELETE FROM Singers WHERE SingerId > 10")
print("{} record(s) deleted.".format(row_ct))
Ruby
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.
# project_id = "Your Google Cloud project ID"
# instance_id = "Your Spanner instance ID"
# database_id = "Your Spanner database ID"
require"google/cloud/spanner"
spanner=Google::Cloud::Spanner .newproject:project_id
client=spanner.client instance_id,database_id
row_count=client.execute_partition_update (
"DELETE FROM Singers WHERE SingerId > 10"
)
puts"#{row_count} records deleted."
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.