// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService =
user.getService(DfpService.V201311.CUSTOM_TARGETING_SERVICE);
// Set ID of the custom targeting key to delete values from.
long customTargetingKeyId = Long.parseLong("INSERT_CUSTOM_TARGETING_KEY_ID_HERE");
// Create statement to only select custom values by the given custom
// targeting key ID.
String statementText = "WHERE customTargetingKeyId = :customTargetingKeyId";
StatementBuilder statementBuilder = new StatementBuilder("")
.putValue("customTargetingKeyId", customTargetingKeyId);
// Set defaults for page and offset.
CustomTargetingValuePage page = new CustomTargetingValuePage();
int offset = 0;
List<Long> customTargetingValueIds = new ArrayList<Long>();
do {
// Create a statement to page through custom targeting values.
statementBuilder.setQuery(statementText + " LIMIT 500 OFFSET " + offset);
// Get custom targeting values by statement.
page = customTargetingService.getCustomTargetingValuesByStatement(
statementBuilder.toStatement());
if (page.getResults() != null) {
for (CustomTargetingValue customTargetingValue : page.getResults()) {
customTargetingValueIds.add(customTargetingValue.getId());
}
}
offset += 500;
} while (offset < page.getTotalResultSetSize());
System.out.println(
"Number of custom targeting values to be deleted: " + customTargetingValueIds.size());
if (customTargetingValueIds.size() > 0) {
// Modify statement for action.
statementBuilder.setQuery("WHERE customTargetingKeyId = :customTargetingKeyId AND id IN ("
+ StringUtils.join(customTargetingValueIds, ",") + ")");
// Create action.
DeleteCustomTargetingValues action = new DeleteCustomTargetingValues();
// Perform action.
UpdateResult result = customTargetingService.performCustomTargetingValueAction(
action, statementBuilder.toStatement());
// Display results.
if (result != null && result.getNumChanges() > 0) {
System.out.println("Number of custom targeting values deleted: "