// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService =
dfpServices.get(session, CustomTargetingServiceInterface.class);
// Create a statement to select a custom targeting key.
StatementBuilder statementBuilder = new StatementBuilder()
.where("WHERE id = :id")
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("id", customTargetingKeyId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get custom targeting keys by statement.
CustomTargetingKeyPage page = customTargetingService.getCustomTargetingKeysByStatement(
statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CustomTargetingKey customTargetingKey : page.getResults()) {
System.out.printf("%d) Custom targeting key with ID \"%d\""
+ " will be deleted.\n", i++, customTargetingKey.getId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of custom targeting keys to be deleted: %d\n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201311.DeleteCustomTargetingKeys action =
new com.google.api.ads.dfp.axis.v201311.DeleteCustomTargetingKeys();
// Perform action.
UpdateResult result = customTargetingService.performCustomTargetingKeyAction(
action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of custom targeting keys deleted: %d\n", result.getNumChanges());
} else {
System.out.println("No custom targeting keys were deleted.");