// Set the name of the custom targeting key to delete.
String customTargetingKeyName = "INSERT_CUSTOM_TARGETING_KEY_NAME_HERE";
// Create statement to only select custom targeting key by the given name.
String statementText = "WHERE name = :name";
StatementBuilder statementBuilder = new StatementBuilder("")
.putValue("name", customTargetingKeyName);
// Set defaults for page and offset.
CustomTargetingKeyPage page = new CustomTargetingKeyPage();
int offset = 0;
List<Long> customTargetingKeyIds = new ArrayList<Long>();
do {
// Create a statement to page through custom targeting keys.
statementBuilder.setQuery(statementText + " LIMIT 500 OFFSET " + offset);
// Get custom targeting keys by statement.
page = customTargetingService.getCustomTargetingKeysByStatement(
statementBuilder.toStatement());
if (page.getResults() != null) {
for (CustomTargetingKey customTargetingKey : page.getResults()) {
customTargetingKeyIds.add(customTargetingKey.getId());
}
}
offset += 500;
} while (offset < page.getTotalResultSetSize());
System.out.println(
"Number of custom targeting keys to be deleted: " + customTargetingKeyIds.size());
if (customTargetingKeyIds.size() > 0) {
// Modify statement for action.
statementBuilder.setQuery(
"WHERE id IN (" + StringUtils.join(customTargetingKeyIds, ",") + ")");
// Create action.
DeleteCustomTargetingKeys action = new DeleteCustomTargetingKeys();
// Perform action.
UpdateResult result = customTargetingService.performCustomTargetingKeyAction(
action, statementBuilder.toStatement());
// Display results.
if (result != null && result.getNumChanges() > 0) {
System.out.println("Number of custom targeting keys deleted: " + result.getNumChanges());
} else {