How do I clean messages out of a rocketchat room or channel?

We switched from a paid slack account last year, to rocketchat.

I've written bots in the past to alert coworkers of system issues in a more public place, but some of my rooms were getting upwards of 10,000 messages and the browser and phones would be extremely slow pulling the room up as message counts were growing.

Sending messages to a chat in a controlled manor is a great way to alert a lot of people to issues, or log something. This should make it super easy to clean the rooms out when they grow larger than you thought they would!

python to the rescue.

My client can do the following:

  • test connection to server (--whoami)
  • send a message to a user. (--send --user justin --text "hi there!")
  • send a message to a group. (--send --group groupname --text "Hi there!")
  • send a message to a channel. (--send --channel channelname --text "Hi there!"
  • delete all messages in a group. (--delete --group roomname --iknow)
  • delete all messages in a channel. (--delete --channel roomname --iknow)
  • install saved rocketchat credentials. (--install --rcuser user --rcpass pass --rchost hostname)
  • spam a group with test messages (--trash --iknow --group groupname)
  • spam a channel with test messages (--trash --iknow --channel channelname)

What are these groups and channels?

On rocketchat it gets a little confusing sometimes. If you have a room, where everyone is allowed to view and post, it's considered a channel.

If you have a private channel, it's considered a group.

I found a newer api, as my last client was using rocket-python, which didnt seem to allow me to delete messages in a somewhat controlled manor.

The api can be installed like so

python -m pip install cosmicray-rocketchat

to use the software paste the following in a file, name it


import rocketchat
import argparse
import datetime

parser = argparse.ArgumentParser()
parser.add_argument('--rcuser', help='The username used with rocketchat, Only needs to be set once.')
parser.add_argument('--rcpass', help='The password used with rocketchat. Only needs to be set once.')
parser.add_argument('--rchost', help='Chat host to use for API access')
parser.add_argument('--install', help='create config file on this machine for access, pass --rcuser ( --rcpass (rcpassword)  and --rchost',action='store_true')
parser.add_argument('--channel', help='channel to send to/operate on')
parser.add_argument('--group', help='group to send to/operate on')
parser.add_argument('--user', help='user to send to/operate on')
parser.add_argument('--whoami', help='test of connection to rocketchat api', action='store_true')
parser.add_argument('--delete', help='delete messages from group or channel', action='store_true')
parser.add_argument('--verbose', help='enable more logging to console')
parser.add_argument('--send', help='text to send to room, user, or channel', action='store_true')
parser.add_argument('--iknow', help='required for deletion of messages.', action='store_true')
parser.add_argument('--text', help='text to send to room, user, or channel')
parser.add_argument('--trash',help='user with --iknow to spam a room with messages, mainly used for testing', action='store_true')
args = parser.parse_args()

# store variables
whoami = args.whoami
text = args.text
user = args.user
group =
channel =
sendmsg = args.send
install = args.install
rcuser = args.rcuser
rcpass = args.rcpass
rchost = args.rchost
verbose = args.verbose
iknow = args.iknow
delete = args.delete
now =
trash = args.trash

# first setup, creates local file with config data defaults.
if install == True:
    if rcuser != None:
        if rcpass != None:
            if rchost != None:
                if verbose == True:
                    print("storing config")

# prints basic info about logged in bot user, used to test config
if whoami == True:

#send message code
if sendmsg == True:
    if channel != None:
        if text != None:
            myroom = rocketchat.channels()[channel]
    if user != None:
        if text != None:
            users = rocketchat.users()
            foo = users[user]
    if group != None:
        if text != None:
            mygroup = rocketchat.groups()[group]

# channel cleaning
if iknow == True:
    if delete == True:
        if channel != None:
            while True:
                    myroom = rocketchat.channels()[channel]
                    message = myroom.messages.last[0]
                    myroom.send("Room cleaned: " + str(now))
        if group != None:
            while True:
                    myroom = rocketchat.groups()[group]
                    message = myroom.messages.last[0]
                    myroom.send("Room cleaned: " + str(now) )

# room trashers for testing deletes
if trash == True:
    if channel != None:
        if iknow == True:
            count = 0
            mychannel = rocketchat.channels()[channel]
            while True:
                mychannel.send('*testmessage* ' + str(count))
                count += 1
                if count == 3000:
                    mychannel.send('room successfully trashed')

    if group != None:
        if iknow == True:
            count = 0
            mygroup = rocketchat.groups()[group]
            while True:
                mygroup.send('*testmessage* ' + str(count))
                count += 1
                if count == 3000:
                    mygroup.send('room successfully trashed')
                    if verbose == True:
                        print('room trashed successfully!')

once saved, use the following examples:

first install example:
./ --install --rcuser rocketchatuser --rcpass rocketchatpass --rchost

delete messages from channel:
./ --delete --iknow --channel general

delete messages from room:
./ --delete --iknow --group mygroup

send a message to a group:
./ --send --group mygroup --text "Hello there"

send a message to a channel:
./ --send --channel general --text "Hello there"

send a message to a user:
/ --send --user username --text "Hello there"

fill a channel with test messages:

./ --trash --channel general 

file a group with test messages:

./ --trash --group mygroup
comments powered by Disqus