# Trim Value File

**URL:** <https://discuss.dgraph.io/t/trim-value-file/2577>\
**Category:** Badger\
**Created:** [May 12, 2018, 2:22pm UTC](https://discuss.dgraph.io/t/trim-value-file/2577 "2018-05-12T14:22:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dc0d](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dc0d/32/813_2.png) [@dc0d](https://discuss.dgraph.io/u/dc0d)\
**Post date:** [May 12, 2018, 2:22pm UTC](https://discuss.dgraph.io/t/trim-value-file/2577/1 "2018-05-12T14:22:45Z")

</div>

How to trim value file of `badger` after deleting (almost) all entries?

---

<div class="post-metadata">

**Author:** ![atul\_nasir](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@atul\_nasir](https://discuss.dgraph.io/u/atul_nasir)\
**Post date:** [January 5, 2025, 8:13pm UTC](https://discuss.dgraph.io/t/trim-value-file/2577/2 "2025-01-05T20:13:57Z")

</div>

```auto
package main

import (
    "context"
    "fmt"
    "log"

    "github.com/dgraph-io/dgo/v2"
    "github.com/dgraph-io/dgo/v2/protos/api"
    "google.golang.org/grpc"
)

func main() {
    conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))

    // Step 1: Delete almost all entries (example deletion)
    deleteEntries(dg)

    // Step 2: Run garbage collection on the value log
    if err := runValueLogGC(dg); err != nil {
        log.Fatalf("Failed to run value log GC: %v", err)
    }

    fmt.Println("Value log GC completed.")
}

func deleteEntries(dg *dgo.Dgraph) {
    ctx := context.Background()
    txn := dg.NewTxn()
    defer txn.Discard(ctx)

    // Example: Delete all nodes with a specific predicate
    query := `
    mutation {
        delete {
            <someUID> * *
        }
    }
    `

    _, err := txn.Mutate(ctx, &api.Mutation{
        DelNquads: []byte(query),
        CommitNow: true,
    })
    if err != nil {
        log.Fatalf("Error deleting entries: %v", err)
    }
}

func runValueLogGC(dg *dgo.Dgraph) error {
    ctx := context.Background()
    
    // This operation requires admin permissions, ensure you have those.
    response, err := dg.Alter(ctx, &api.Operation{
        RunValueLogGC: true,
    })
    if err != nil {
        return fmt.Errorf("failed to run value log GC: %v", err)
    }

    if response.Error != nil {
        return fmt.Errorf("GC operation returned an error: %s", response.Error)
    }

    return nil
}

```
