Skip to content

Instantly share code, notes, and snippets.

@dtjm
Created November 8, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtjm/646ec43cc00efb4b6683 to your computer and use it in GitHub Desktop.
Save dtjm/646ec43cc00efb4b6683 to your computer and use it in GitHub Desktop.
Delete all message in a Slack channel
package main
import (
"log"
"github.com/nlopes/slack"
)
const (
apiToken = "API TOKEN"
channelName = "channel name" // without the octothorpe "#"
)
func checkErr(err error, msg string) {
if err != nil {
log.Fatalf(msg, err.Error())
}
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
c := slack.New(apiToken)
list, err := c.GetChannels(true)
checkErr(err, "GetChannels: %s")
var generalChannel slack.Channel
for _, c := range list {
if c.Name == channelName {
generalChannel = c
break
}
}
hasMore := true
for hasMore {
hist, err := c.GetChannelHistory(generalChannel.ID, slack.HistoryParameters{Count: 10000000})
checkErr(err, "GetChannelHistory: %s")
hasMore = hist.HasMore
log.Printf("got %d messages", len(hist.Messages))
for _, m := range hist.Messages {
_, _, err = c.DeleteMessage(generalChannel.ID, m.Timestamp)
checkErr(err, "DeleteMessage: %s")
log.Printf("deleted %q", m.Text)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment