Create a column family that has a union garbage-collection policy (age and versions).

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Configure garbage collection](https://docs.cloud.google.com/bigtable/docs/configuring-garbage-collection)

## Code sample

### C++


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    namespace cbt = ::google::cloud::bigtable;
    namespace cbta = ::google::cloud::bigtable_admin;
    using ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest;
    using ::google::cloud::StatusOr;
    [](cbta::BigtableTableAdminClient admin, std::string const& project_id,
       std::string const& instance_id, std::string const& table_id,
       std::string const& family_name) {
      std::string table_name = cbt::TableName(project_id, instance_id, table_id);
      auto constexpr kSecondsPerDay =
          std::chrono::seconds(std::chrono::hours(24)).count();

      ModifyColumnFamiliesRequest::Modification mod;
      mod.set_id(family_name);
      auto& gc_union = *mod.mutable_create()->mutable_gc_rule()->mutable_union_();
      gc_union.add_rules()->set_max_num_versions(1);
      gc_union.add_rules()->mutable_max_age()->set_seconds(5 * kSecondsPerDay);

      StatusOr<google::bigtable::admin::v2::Table> schema =
          admin.ModifyColumnFamilies(table_name, {std::move(mod)});

      if (!schema) throw std::move(schema).status();
      std::cout << "Schema modified to: " << schema->DebugString() << "\n";
    }

### C#


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Create a column family with GC policy to drop data that matches at least one condition.
    // Initialize request argument(s).
    // Define a GC rule to drop cells older than 5 days or not the most recent version.
    GcRule.Types.Union unionRule = new GcRule.Types.Union
    {
        Rules =
        {
            new GcRule { MaxNumVersions = 1 },
            new GcRule { MaxAge = Duration.FromTimeSpan(TimeSpan.FromDays(5)) }
        }
    };
    GcRule gcRule = new GcRule { Union = unionRule };

    // Column family to create
    ColumnFamily columnFamily = new ColumnFamily { GcRule = gcRule };

    TableName tableName = new TableName(projectId, instanceId, tableId);

    // Modification to create column family
    ModifyColumnFamiliesRequest.Types.Modification modification = new ModifyColumnFamiliesRequest.Types.Modification
    {
        Create = columnFamily,
        Id = "cf3"
    };

    ModifyColumnFamiliesRequest request = new ModifyColumnFamiliesRequest
    {
        TableName = tableName,
        Modifications = { modification }
    };
    try
    {
        // Make the request
        Table response = bigtableTableAdminClient.ModifyColumnFamilies(request);
        Console.WriteLine("Created column family");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error creating column family {ex.Message}");
    }

### Go


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"

    	"cloud.google.com/go/bigtable"
    )

    func createFamilyGCUnion(w io.Writer, projectID, instanceID string, tableName string) error {
    	// projectID := "my-project-id"
    	// instanceID := "my-instance-id"
    	// tableName := "my-table-name"

    	ctx := context.Background()

    	adminClient, err := bigtable.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_AdminClient_NewAdminClient(ctx, projectID, instanceID)
    	if err != nil {
    		return fmt.Errorf("bigtable.NewAdminClient: %w", err)
    	}
    	defer adminClient.Close()

    	columnFamilyName := "cf3"
    	if err := adminClient.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_AdminClient_CreateColumnFamily(ctx, tableName, columnFamilyName); err != nil {
    		return fmt.Errorf("CreateColumnFamily(%s): %w", columnFamilyName, err)
    	}

    	// Define a GC rule to drop cells older than 5 days or not the most recent version
    	maxAge := time.Hour * 24 * 5
    	maxAgePolicy := bigtable.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_GCPolicy_MaxAgePolicy(maxAge)
    	policy := bigtable.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_GCPolicy_UnionPolicy(bigtable.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_GCPolicy_MaxVersionsPolicy(2), maxAgePolicy)
    	if err := adminClient.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigtable/latest/index.html#cloud_google_com_go_bigtable_AdminClient_SetGCPolicy(ctx, tableName, columnFamilyName, policy); err != nil {
    		return fmt.Errorf("SetGCPolicy(%s): %w", policy, err)
    	}

    	fmt.Fprintf(w, "created column family %s with policy: %v\n", columnFamilyName, policy)
    	return nil
    }

### Java


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Creates a column family with GC policy to drop data that matches at least one condition.

    // Defines a list of GC rules to drop cells older than 5 days OR not the most recent
    // version.
    GcRule unionRule =
        GcRuleBuilder.union()
            .add(GcRuleBuilder.maxAge(Duration.ofDays(5)))
            .add(GcRuleBuilder.maxVersions(1))
            .build();

    // Creates column family with given GC rule.
    try {
      String tableName =
          "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId;
      ModifyColumnFamiliesRequest request =
          ModifyColumnFamiliesRequest.newBuilder()
              .setName(tableName)
              .addModifications(
                  ModifyColumnFamiliesRequest.Modification.newBuilder()
                      .setId(COLUMN_FAMILY_3)
                      .setCreate(ColumnFamily.newBuilder().setGcRule(unionRule)))
              .build();
      adminClient.modifyColumnFamilies(request);
      System.out.println("Created column family: " + COLUMN_FAMILY_3);
    } catch (AlreadyExistsException e) {
      System.err.println(
          "Failed to create column family with rule, already exists: " + e.getMessage());
    }

### PHP


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;
    use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
    use Google\Cloud\Bigtable\Admin\V2\GcRule;
    use Google\Cloud\Bigtable\Admin\V2\GcRule\Union as GcRuleUnion;
    use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest;
    use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
    use Google\Protobuf\Duration;

    /**
     * Create a new column family with a union GC rule
     *
     * @param string $projectId The Google Cloud project ID
     * @param string $instanceId The ID of the Bigtable instance where the table resides
     * @param string $tableId The ID of the table in which the rule needs to be created
     */
    function create_family_gc_union(
        string $projectId,
        string $instanceId,
        string $tableId
    ): void {
        $tableAdminClient = new BigtableTableAdminClient();

        $tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);

        print('Creating column family cf3 with union GC rule...' . PHP_EOL);
        // Create a column family with GC policy to drop data that matches
        // at least one condition.
        // Define a GC rule to drop cells older than 5 days or not the
        // most recent version

        $columnFamily3 = new ColumnFamily();

        $ruleUnion = new GcRuleUnion();
        $ruleUnionArray = [
            (new GcRule())->setMaxNumVersions(2),
            (new GcRule())->setMaxAge((new Duration())->setSeconds(3600 * 24 * 5))
        ];
        $ruleUnion->setRules($ruleUnionArray);
        $union = new GcRule();
        $union->setUnion($ruleUnion);

        $columnFamily3->setGCRule($union);

        $columnModification = new Modification();
        $columnModification->setId('cf3');
        $columnModification->setCreate($columnFamily3);
        $modifyColumnFamiliesRequest = (new ModifyColumnFamiliesRequest())
            ->setName($tableName)
            ->setModifications([$columnModification]);
        $tableAdminClient->modifyColumnFamilies($modifyColumnFamiliesRequest);

        print('Created column family cf3 with Union GC rule.' . PHP_EOL);
    }

### Python


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    from google.protobuf import duration_pb2

    from google.cloud import bigtable_admin_v2

    client = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.overlay.services.bigtable_table_admin.BigtableTableAdminClient.html()
    table_path = client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.overlay.services.bigtable_table_admin.BigtableTableAdminClient.html#google_cloud_bigtable_admin_v2_overlay_services_bigtable_table_admin_BigtableTableAdminClient_table_path(project_id, instance_id, table_id)

    print("Creating column family cf3 with union GC rule...")
    # Create a column family with GC policy to drop data that matches
    # at least one condition.
    # Define a GC rule to drop cells older than 5 days or not the
    # most recent version
    union_rule = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.GcRule.html(
        union=bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.GcRule.html.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.GcRule.Union.html(
            rules=[
                bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.GcRule.html(
                    max_age=duration_pb2.Duration(seconds=5 * 86400)
                ),
                bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.GcRule.html(max_num_versions=2),
            ]
        )
    )
    cf3 = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.ColumnFamily.html(gc_rule=union_rule)
    mod3 = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.ModifyColumnFamiliesRequest.html.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.ModifyColumnFamiliesRequest.Modification.html(
        id="cf3", create=cf3
    )
    client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.overlay.services.bigtable_table_admin.BigtableTableAdminClient.html#google_cloud_bigtable_admin_v2_overlay_services_bigtable_table_admin_BigtableTableAdminClient_modify_column_families(name=table_path, modifications=[mod3])
    print("Created column family cf3 with Union GC rule")

### Ruby


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    # Create a column family with GC policy to drop data that matches at least
    # one condition
    max_age_rule = Google::Cloud::Bigtable::GcRule.max_age 60 * 60 * 24 * 5
    max_versions_rule = Google::Cloud::Bigtable::GcRule.max_versions 2
    union_gc_rule = Google::Cloud::Bigtable::GcRule.union max_age_rule, max_versions_rule
    column_families = table.column_families do |cfs|
      cfs.add "cf3", gc_rule: union_gc_rule
    end
    family = column_families["cf3"]

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigtable).