maddyblue
maddyblue

Reputation: 16882

How do I delete all blobs in app engine on Go?

The blobstore API has no function to list all blobs. How can I get this list and then delete all blobs?

Upvotes: 2

Views: 226

Answers (1)

maddyblue
maddyblue

Reputation: 16882

The blobstore API on appengine for go has no way to do this. Instead, use the datastore to fetch __BlobInfo__ entities as appengine.BlobInfo. Although the API claims to have a BlobKey field, it is not populated. Instead, use the string ID of the returned key and cast it to an appengine.BlobKey, which you can then pass to blobstore.Delete.

Here's a handler at "/tasks/delete-blobs" to delete 20k blobs at a time in a loop until they are all deleted. Also note that cursors are not used here. I suspect that __BlobInfo__ is special and doesn't support cursors. (When I attempted to use them, they did nothing.)

func DeleteBlobs(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    c = appengine.Timeout(c, time.Minute)
    q := datastore.NewQuery("__BlobInfo__").KeysOnly()
    it := q.Run(ctx)
    wg := sync.WaitGroup{}
    something := false
    for _i := 0; _i < 20; _i++ {
        var bk []appengine.BlobKey
        for i := 0; i < 1000; i++ {
            k, err := it.Next(nil)
            if err == datastore.Done {
                break
            } else if err != nil {
                c.Errorf("err: %v", err)
                continue
            }
            bk = append(bk, appengine.BlobKey(k.StringID()))
        }
        if len(bk) == 0 {
            break
        }
        go func(bk []appengine.BlobKey) {
            something = true
            c.Errorf("deleteing %v blobs", len(bk))
            err := blobstore.DeleteMulti(ctx, bk)
            if err != nil {
                c.Errorf("blobstore delete err: %v", err)
            }
            wg.Done()
        }(bk)
        wg.Add(1)
    }
    wg.Wait()
    if something {
        taskqueue.Add(c, taskqueue.NewPOSTTask("/tasks/delete-blobs", nil), "")
    }
}

Upvotes: 5

Related Questions