ZetCode

Golang delete function

last modified May 8, 2025

This tutorial explains how to use the delete built-in function in Go. We'll cover map basics with practical examples of element removal from maps.

The delete function is used to remove elements from a map in Go. It takes a map and a key as arguments and removes the key-value pair. If the key doesn't exist, delete does nothing without any error.

In Go, delete is used with maps to manage their contents dynamically. It's a safe operation that doesn't panic if the key is missing from the map.

Basic map deletion example

The simplest use of delete removes a key-value pair from a map. This example demonstrates basic map deletion.
Note: Delete modifies the original map, not a copy.

basic_delete.go
package main

import "fmt"

func main() {
    m := map[string]int{
        "apple":  5,
        "banana": 7,
        "orange": 9,
    }
    
    fmt.Println("Before delete:", m)
    
    delete(m, "banana") // Remove the "banana" key
    
    fmt.Println("After delete:", m)
}

The "banana" key is removed from the map. The original map is modified. The remaining keys "apple" and "orange" stay unchanged.

Deleting non-existent keys

Deleting a key that doesn't exist is safe in Go. This example shows that delete does nothing when the key is missing.

nonexistent_key.go
package main

import "fmt"

func main() {
    m := map[int]string{
        1: "one",
        2: "two",
        3: "three",
    }
    
    fmt.Println("Before delete:", m)
    
    delete(m, 4) // Try to delete non-existent key
    
    fmt.Println("After delete:", m) // Map remains unchanged
}

The delete operation doesn't affect the map when the key doesn't exist. No error or panic occurs, making it safe to use in all cases.

Checking before deletion

We can check if a key exists before deleting it. This example shows how to safely delete only existing keys.

check_before_delete.go
package main

import "fmt"

func main() {
    m := map[string]float64{
        "pi": 3.14159,
        "e":  2.71828,
    }
    
    key := "phi"
    
    if _, exists := m[key]; exists {
        delete(m, key)
        fmt.Println("Deleted", key)
    } else {
        fmt.Println(key, "not found")
    }
    
    fmt.Println("Final map:", m)
}

The code checks for key existence before attempting deletion. This pattern is useful when you need to confirm deletion success.

Deleting from nested maps

Deleting from nested maps requires careful handling. This example shows how to delete from a map within a map.

nested_map.go
package main

import "fmt"

func main() {
    users := map[string]map[string]bool{
        "john": {
            "admin": true,
            "active": true,
        },
        "jane": {
            "admin": false,
            "active": true,
        },
    }
    
    fmt.Println("Before delete:", users)
    
    // Delete the "admin" key from john's properties
    if user, exists := users["john"]; exists {
        delete(user, "admin")
    }
    
    fmt.Println("After delete:", users)
}

We first check if the outer map key exists, then delete from the inner map. This two-step process ensures safe operation on nested structures.

Deleting in concurrent scenarios

When using maps with goroutines, proper synchronization is needed. This example demonstrates safe deletion in concurrent code.

concurrent_delete.go
package main

import (
    "fmt"
    "sync"
)

func main() {
    var m sync.Map
    m.Store("a", 1)
    m.Store("b", 2)
    m.Store("c", 3)
    
    var wg sync.WaitGroup
    wg.Add(2)
    
    // First goroutine deletes "a"
    go func() {
        defer wg.Done()
        m.Delete("a")
        fmt.Println("Deleted a")
    }()
    
    // Second goroutine deletes "b"
    go func() {
        defer wg.Done()
        m.Delete("b")
        fmt.Println("Deleted b")
    }()
    
    wg.Wait()
    
    // Show remaining keys
    m.Range(func(key, value interface{}) bool {
        fmt.Printf("%v: %v\n", key, value)
        return true
    })
}

The sync.Map provides concurrent-safe operations. We use Delete method instead of the built-in delete function.

Source

Go language specification

This tutorial covered the delete function in Go with practical examples of map manipulation and element removal.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.