You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Google Ads Python客户端库清空指定客户匹配列表所有数据?

如何用Google Ads Python客户端库清空Customer Match列表数据(不删除列表)

要实现这个需求,核心是调用Google Ads API的CustomerMatchUserListService,发送**REMOVE_ALL**类型的操作请求,无需提供具体用户数据,仅需指定目标列表ID即可。

完整代码示例

from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException

def clear_customer_match_list(client, customer_id, user_list_id):
    # 获取对应的服务和操作类型
    customer_match_user_list_service = client.get_service("CustomerMatchUserListService")
    customer_match_user_list_operation = client.get_type("CustomerMatchUserListOperation")
    
    # 指定执行清空操作
    customer_match_user_list_operation.remove_all = True
    
    # 构造目标列表的资源名称(API要求的格式)
    user_list_resource_name = customer_match_user_list_service.user_list_path(
        customer_id, user_list_id
    )
    customer_match_user_list_operation.resource_name = user_list_resource_name
    
    try:
        # 发送清空请求
        response = customer_match_user_list_service.mutate_customer_match_user_list(
            customer_id=customer_id, operation=customer_match_user_list_operation
        )
        print(f"成功清空列表:{response.result.resource_name}")
        return response
    except GoogleAdsException as ex:
        print(f"操作失败:")
        for error in ex.failure.errors:
            print(f"错误码:{error.error_code}")
            print(f"错误信息:{error.message}")

if __name__ == "__main__":
    # 替换为你的实际配置和ID
    GOOGLE_ADS_CONFIG_PATH = "google-ads.yaml"
    CUSTOMER_ID = "YOUR_CUSTOMER_ID"  # 纯数字格式,不要带横杠
    USER_LIST_ID = "YOUR_USER_LIST_ID"
    
    # 初始化客户端
    client = GoogleAdsClient.load_from_storage(GOOGLE_ADS_CONFIG_PATH)
    clear_customer_match_list(client, CUSTOMER_ID, USER_LIST_ID)

关键代码说明

  • 操作类型配置:通过remove_all = True标记执行批量清空,这是实现一次性删除所有成员的核心参数,不需要传入任何用户数据。
  • 资源名称构造:用user_list_path方法生成API可识别的列表资源路径,确保请求能定位到正确的目标列表。
  • 异常处理:捕获GoogleAdsException可以获取官方返回的错误细节,方便排查权限不足、ID错误等问题。

官方文档核心内容翻译

要一次性清空Customer Match列表的所有数据,需发送REMOVE_ALL类型的用户列表操作。该操作会删除列表内的全部成员,但保留列表本身及其所有属性(如名称、描述、状态等)。与逐条删除用户数据的方式不同,此操作是原子性的,能够高效处理大规模用户列表。

注意事项

  • 确保你的Google Ads账号拥有用户列表修改权限,否则会返回权限相关错误。
  • 该操作不可逆,执行前请确认目标列表ID正确。
  • 操作完成后,列表的元数据(如创建时间、列表类型)不会改变,仅成员数据被清空。

内容的提问来源于stack exchange,提问作者Amateur programmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:42:15