Skip to content

Cannot delete blocks #24007

Closed
Closed
@heliosVII-tech

Description

@heliosVII-tech

For a particular reason I want to delete blocks. But when I call rawdb.DeleteBlock it doesn't actually delete the old block. For example, the code below prints the same value for blockBefore and blockAfter (sans pointer differences). How do I actually delete blocks to save disk space?

func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
	hash := rawdb.ReadCanonicalHash(bc.db, number)
	if hash == (common.Hash{}) {
		return nil
	}

	if number == 1000 {
		blockBefore := bc.GetBlock(hash, number)

                 // clear from cache
		present := bc.blockCache.Remove(hash)
                 bc.blockCache.Purge()

                // try this way
		rawdb.DeleteBlock(bc.db, hash, number)

                // try using batches
		batch := bc.db.NewBatch()
		i := 0
		for _, hash := range rawdb.ReadAllHashes(bc.db, number) {
			i++
			rawdb.DeleteBlock(batch, hash, number)
		}
		if err := batch.Write(); err != nil {
			log.Error("Batch write got error", "err", err.Error())
		}

		blockAfter := bc.GetBlock(hash, number)
		log.Info("GetHeader", "number", number, "blockBefore", blockBefore, "blockAfter", blockAfter, "present", present, "i", i)
	}

	return bc.GetBlock(hash, number)
}

Is there something obvious missing here? I see that sidechains are deleted in a similar manner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions