Search code examples
gogo-gorm

Gorm for batch deletion


Golang uses the Gorm framework for batch deletion. How to write this statement?

func DeleteUsers(id []int64) error {
    return db.Table("users").Delete(id).Error
}

After using the above method, all the data in the table will be cleared.


Solution

  • You can update the code as below to solve the issue.

    func DeleteUsers(id []int64) error {
        return db.Table("users").Where("users.id", id).Delete(struct{}{}).Error
    }