Remove an extension setting

Extension setting services use feeds to control ad extensions. ExtensionFeedItem is a wrapper over a FeedItem that handles much of the feed mechanics for you. Still, there are some cleanup that are required when removing an extension setting.

A functional extension setting is made up of both some set of extension feed items and either one CustomerExtensionSetting, CampaignExtensionSetting, or AdGroupExtensionSetting. You can delete the extension setting, which will remove the association between the extension feed items and that resource, but the extension feed items will still exist and can be reused with other extension settings in the future. You can also update an extension setting to use a different set of extension feed items, but again unused feed items will continue to exist.

To remove extension feed items that are no longer associated with any extension settings, make a followup call to the ExtensionFeedItemService.

Remove the entire extension setting

The example below assumes we want to remove both the extension setting and extension feed item that were created earlier. Removing the extension setting doesn't automatically remove the extension feed items, so that must be done in a separate step. In our example, all removal operations are sent using a multiresource mutate request, GoogleAdsService.Mutate(), to ensure they're executed atomically.

PHP

public static function runExample(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    int $campaignId
) {
    $mutateOperations = [];
    // Creates a mutate operation that contains the campaign extension setting operation
    // to remove the specified sitelink campaign extension setting.
    $mutateOperations[] =
        self::createSitelinkCampaignExtensionSettingMutateOperation($customerId, $campaignId);

    // Gets all sitelink extension feed items of the specified campaign.
    $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
    $extensionFeedItemResourceNames = self::getAllSitelinkExtensionFeedItems(
        $googleAdsServiceClient,
        $customerId,
        $campaignId
    );

    // Creates mutate operations, each of which contains an extension feed item operation
    // to remove the specified extension feed items.
    $mutateOperations = array_merge(
        $mutateOperations,
        self::createExtensionFeedItemMutateOperations($extensionFeedItemResourceNames)
    );

    // Issues a mutate request to remove the campaign extension setting and its extension
    // feed items.
    $response = $googleAdsServiceClient->mutate($customerId, $mutateOperations);
    $mutateOperationResponses = $response->getMutateOperationResponses();

    // Prints the information on the removed campaign extension setting and its extension feed
    // items.
    // Each mutate operation response is returned in the same order as we passed its
    // corresponding operation. Therefore, the first belongs to the campaign setting operation,
    // and the rest belong to the extension feed item operations.
    printf(
        "Removed a campaign extension setting with resource name: '%s'.%s",
        $mutateOperationResponses[0]->getCampaignExtensionSettingResult()->getResourceName(),
        PHP_EOL
    );
    for ($i = 1; $i < count($mutateOperationResponses); $i++) {
        printf(
            "Removed an extension feed item with resource name: '%s'.%s",
            $mutateOperationResponses[$i]->getExtensionFeedItemResult()->getResourceName(),
            PHP_EOL
        );
    }
}

Ruby

client = Google::Ads::GoogleAds::GoogleAdsClient.new

operation = client.operation.remove_resource.campaign_extension_setting(extension_setting_resource_name)
client.service.campaign_extension_setting.mutate_campaign_extension_settings(customer_id, [operation])

To fetch the extension feed items of the specified campaign extension setting, send a request for campaign_extension_setting:

PHP

private static function getAllSitelinkExtensionFeedItems(
    GoogleAdsServiceClient $googleAdsServiceClient,
    int $customerId,
    int $campaignId
): array {
    // Creates a query that retrieves all campaigns.
    $query = sprintf(
        "SELECT campaign_extension_setting.campaign, "
        . "campaign_extension_setting.extension_type, "
        . "campaign_extension_setting.extension_feed_items "
        . "FROM campaign_extension_setting "
        . "WHERE campaign_extension_setting.campaign = '%s' "
        . "AND campaign_extension_setting.extension_type = %s",
        ResourceNames::forCampaign($customerId, $campaignId),
        ExtensionType::name(ExtensionType::SITELINK)
    );

    // Issues a search stream request.
    /** @var GoogleAdsServerStreamDecorator $stream */
    $stream = $googleAdsServiceClient->searchStream($customerId, $query);

    $extensionFeedItemResourceNames = [];
    // Iterates over all rows in all messages and prints the requested field values for
    // the campaign extension setting in each row.
    foreach ($stream->iterateAllElements() as $googleAdsRow) {
        /** @var GoogleAdsRow $googleAdsRow */
        $extensionFeedItems =
            $googleAdsRow->getCampaignExtensionSetting()->getExtensionFeedItems();
        foreach ($extensionFeedItems as $extensionFeedItem) {
            /** @var StringValue $extensionFeedItem */
            $extensionFeedItemResourceName = $extensionFeedItem->getValue();
            $extensionFeedItemResourceNames[] = $extensionFeedItemResourceName;
            printf(
                "Extension feed item with resource name '%s' was found.%s",
                $extensionFeedItemResourceName,
                PHP_EOL
            );
        }
    }
    if (empty($extensionFeedItemResourceNames)) {
        throw new \InvalidArgumentException(
            'The specified campaign does not contain a sitelink campaign extension setting.'
        );
    }
    return $extensionFeedItemResourceNames;
}

Remove an extension feed item from an existing setting

Alternatively, if you want to keep the extension setting but change which extension feed items it uses, you can update the extension setting and set the full set of extension settings to use. Remember that this doesn't completely remove the extension feed item; it just disassociates it from this extension setting.

C#

public void Run(GoogleAdsClient client, long customerId, long campaignId,
    long[] feedItemIds)
{
    // Get the CampaignExtensionSettingService.
    CampaignExtensionSettingServiceClient campaignExtensionSettingService =
        client.GetService(Services.V5.CampaignExtensionSettingService);

    // Transform the specified extension feed item IDs to an array of resource names.
    IEnumerable<string> extensionFeedItems = feedItemIds.Select(feedItemId =>
        ResourceNames.ExtensionFeedItem(customerId, feedItemId));

    // Create a campaign extension setting using the specified campaign ID and extension
    // feed item resource names.
    CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting
    {
        ResourceName = ResourceNames.CampaignExtensionSetting(customerId, campaignId,
            ExtensionType.Sitelink)
    };
    campaignExtensionSetting.ExtensionFeedItems.Add(extensionFeedItems);

    // Construct an operation that will update the extension feed item using the FieldMasks
    // utilities to derive the update mask. This mask tells the Google Ads API which
    // attributes of the extension feed item you want to change.
    CampaignExtensionSettingOperation campaignExtensionSettingOperation =
        new CampaignExtensionSettingOperation
        {
            Update = campaignExtensionSetting,
            UpdateMask = FieldMasks.AllSetFieldsOf(campaignExtensionSetting)
        };

    try
    {
        // Issue a mutate request to update the campaign extension setting.
        MutateCampaignExtensionSettingsResponse response =
            campaignExtensionSettingService.MutateCampaignExtensionSettings
                (customerId.ToString(), new[] {campaignExtensionSettingOperation});

        // Print the resource name of the updated campaign extension setting.
        Console.WriteLine("Updated a campaign extension setting with resource name " +
            $"'{response.Results.First().ResourceName}'.");
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}

PHP

public static function runExample(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    int $campaignId,
    array $feedItemIds
) {
    // Transforms the specified feed item IDs to resource names and an array of StringValue, as
    // required by the API.
    $extensionFeedItems = array_map(function ($feedItemId) use ($customerId) {
        return new StringValue([
            'value' => ResourceNames::forExtensionFeedItem($customerId, $feedItemId)
        ]);
    }, $feedItemIds);

    // Creates a campaign extension setting using the specified campaign ID and extension feed
    // item resource names.
    $campaignExtensionSetting = new CampaignExtensionSetting([
        'resource_name' => ResourceNames::forCampaignExtensionSetting(
            $customerId,
            $campaignId,
            ExtensionType::SITELINK
        ),
        'extension_feed_items' => $extensionFeedItems
    ]);

    // Constructs an operation that will update the campaign extension setting, using the
    // FieldMasks utility to derive the update mask. This mask tells the Google Ads API which
    // attributes of the campaign extension setting you want to change.
    $campaignExtensionSettingOperation = new CampaignExtensionSettingOperation();
    $campaignExtensionSettingOperation->setUpdate($campaignExtensionSetting);
    $campaignExtensionSettingOperation->setUpdateMask(
        FieldMasks::allSetFieldsOf($campaignExtensionSetting)
    );

    // Issues a mutate request to update the campaign extension setting.
    $campaignExtensionSettingServiceClient =
        $googleAdsClient->getCampaignExtensionSettingServiceClient();
    $response = $campaignExtensionSettingServiceClient->mutateCampaignExtensionSettings(
        $customerId,
        [$campaignExtensionSettingOperation]
    );

    // Prints the resource name of the updated campaign extension setting.
    /** @var CampaignExtensionSetting $updatedCampaignExtensionSetting */
    $updatedCampaignExtensionSetting = $response->getResults()[0];
    printf(
        "Updated a campaign extension setting with resource name: '%s'.%s",
        $updatedCampaignExtensionSetting->getResourceName(),
        PHP_EOL
    );
}

Ruby

client = Google::Ads::GoogleAds::GoogleAdsClient.new

operation = client.operation.update_resource.campaign_extension_setting(extension_setting_resource_name) do |ces|
  # This list does not contain the previous resource name, and only contains a
  # new, separate extension feed item. The campaign extension setting will be
  # updated to remove the old one and add the new one.
  ces.extension_feed_items << new_efi_resource_name
end

client.service.campaign_extension_setting.mutate_campaign_extension_settings(customer_id, [operation])

Perl

sub update_sitelink_campaign_extension_setting {
  my ($api_client, $customer_id, $campaign_id, $feed_item_ids) = @_;

  # Transform the specified extension feed item IDs to the array of resource names.
  my $extension_feed_items = [
    map {
      Google::Ads::GoogleAds::V5::Utils::ResourceNames::extension_feed_item(
        $customer_id, $_)
    } @$feed_item_ids
  ];

  # Create a campaign extension setting using the specified campaign ID and
  # extension feed item resource names.
  my $campaign_extension_setting =
    Google::Ads::GoogleAds::V5::Resources::CampaignExtensionSetting->new({
      resourceName =>
        Google::Ads::GoogleAds::V5::Utils::ResourceNames::campaign_extension_setting(
        $customer_id, $campaign_id, SITELINK
        ),
      extensionFeedItems => $extension_feed_items
    });

  # Construct an operation that will update the campaign extension setting, using
  # the FieldMasks utility to derive the update mask. This mask tells the Google
  # Ads API which attributes of the campaign extension setting you want to change.
  my $campaign_extension_setting_operation =
    Google::Ads::GoogleAds::V5::Services::CampaignExtensionSettingService::CampaignExtensionSettingOperation
    ->new({
      update     => $campaign_extension_setting,
      updateMask => all_set_fields_of($campaign_extension_setting)});

  # Issue a mutate request to update the campaign extension setting.
  my $campaign_extension_setting_response =
    $api_client->CampaignExtensionSettingService()->mutate({
      customerId => $customer_id,
      operations => [$campaign_extension_setting_operation]});

  # Print the resource name of the updated campaign extension setting.
  printf
    "Updated a campaign extension setting with resource name: '%s'.\n",
    $campaign_extension_setting_response->{results}[0]{resourceName};

  return 1;
}

If you want to remove the now unused extension feed item, send a remove operation to MutateExtensionFeedItems.