@@ -251,6 +251,25 @@ static void writeExampleData(DatabaseClient dbClient) {
251
251
}
252
252
// [END spanner_insert_data]
253
253
254
+ // [START spanner_delete_data]
255
+ static void deleteExampleData (DatabaseClient dbClient ) {
256
+ List <Mutation > mutations = new ArrayList <>();
257
+
258
+ // KeySet.all() can be used to delete all the rows in a table.
259
+ mutations .add (Mutation .delete ("Albums" , KeySet .all ()));
260
+
261
+ // KeySet.singleKey() can be used to delete one row at a time.
262
+ for (Singer singer : SINGERS ) {
263
+ mutations .add (
264
+ Mutation .delete ("Singers" ,
265
+ KeySet .singleKey (Key .newBuilder ().append (singer .singerId ).build ())));
266
+ }
267
+
268
+ dbClient .write (mutations );
269
+ System .out .printf ("Records deleted.\n " );
270
+ }
271
+ // [END spanner_delete_data]
272
+
254
273
// [START spanner_query_data]
255
274
static void query (DatabaseClient dbClient ) {
256
275
// singleUse() can be used to execute a single read or query against Cloud Spanner.
@@ -1077,6 +1096,9 @@ static void run(
1077
1096
case "write" :
1078
1097
writeExampleData (dbClient );
1079
1098
break ;
1099
+ case "delete" :
1100
+ deleteExampleData (dbClient );
1101
+ break ;
1080
1102
case "query" :
1081
1103
query (dbClient );
1082
1104
break ;
@@ -1194,6 +1216,7 @@ static void printUsageAndExit() {
1194
1216
System .err .println ("Examples:" );
1195
1217
System .err .println (" SpannerExample createdatabase my-instance example-db" );
1196
1218
System .err .println (" SpannerExample write my-instance example-db" );
1219
+ System .err .println (" SpannerExample delete my-instance example-db" );
1197
1220
System .err .println (" SpannerExample query my-instance example-db" );
1198
1221
System .err .println (" SpannerExample read my-instance example-db" );
1199
1222
System .err .println (" SpannerExample addmarketingbudget my-instance example-db" );
0 commit comments