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

Hi!

How would I go about using this script to delete thousands of archived channels? I have a .txt file with a list of each channel that was pulled using CLI so would I change the first argument to “channel remove” channels.txt? When I try to delete each channel individually I get a “Unable to find channel” even though it is pulled from the CLI verbatim so I’m worried that it will throw the same error with the script. Thanks in advance for your help!