Mattermost, Inc.

Mattermost Recipe: How to process a large amount of items using the Mattermost CLI

Problem

You have a couple thousand users you need to remove from a specific channel.

Solution

  1. Get a list of the user’s email addresses, usernames, or user IDs in a text file, with one entry per line named users.txt. For example:
admin@example.com
user@example.com
another_user@example.com
  1. Create this shell script and give it the name mattermost_process_file.sh:
#!/bin/bash
mattermost_cmd=$1

$ids_file=$2

while read id;  do
	/opt/mattermost/bin/mattermost $mattermost_cmd $id;
done <$ids_file
  1. Run the shell script with the Mattermost CLI command as the first argument and the text file as the second argument. For example:
$ ./mattermost_process_file.sh "channel remove teamname:channelname" users.txt

Discussion

This is a really common problem for Mattermost administrators, and bash’s great scripting functionality solves it very easily. This works for any Mattermost CLI command that accepts an identifier as the final argument, such as channel archive, team add, or user deactivate

Resources

Mattermost Command Line Tools
mattermost_process_file.sh on Gist

If you have any feedback or specific questions about how to use this script please let me know!

2 Likes