7

Slack allows archiving channels. We currently have around 50 channels that are no longer active with 0 users.

How can I efficiently archive these channels without having to join and archive them one by one?

migrated from superuser.com Aug 24 '16 at 3:04

This question came from our site for computer enthusiasts and power users.

  • According to the tag you have chosen, this question is off topic here. I have voted to have it migrated to Web Applications. – CharlieRB Aug 23 '16 at 20:39
4

To actually "slack-archive" the channels, ie the equivalent of running "/archive" within each channel, you'll need some API magic.

First, set up an API token for your user in the relevant team on https://api.slack.com/custom-integrations/legacy-tokens, then go to https://api.slack.com/methods/channels.list/test to get a json-listing of your channels together with the channel ids.

Then use these channel ids to run https://api.slack.com/methods/channels.archive/test for the channels you want to archive.

Here is a quick and dirty bash script (which uses jq) I recently ran on my Mac in order to archive the hundreds of channels that contained "sk-" in the channel name:

TOKEN="replace-this-with-your-user-api-token-in-the-relevant-team"
curl "https://slack.com/api/channels.list?token=$TOKEN&exclude_archived=true&pretty=1" > /tmp/channels.list
IDS=$(cat /tmp/channels.list | jq '.channels[] | select(.name | contains("sk-")) | .id' | sed -e 's/"//g')
for ID in $IDS; do
    URL="https://slack.com/api/channels.archive?token=$TOKEN&channel=$ID&pretty=1"
    echo $URL
    curl "$URL"
done

It got the job done, archiving about 2-3 channels per second.

  • @pnuts Only briefly now since you linked to them, but I found none that were related to this post? – Motin Oct 17 '17 at 23:23
  • I wanted to archive private conversations, so I needed to switch from legacy channels.list to the newer conversations.list and used the param &types=private_channel. Ditto changing channels.archive to conversations.archive – Carl Walsh Jun 12 at 23:59
1

Go into Team Settings > Message Archives. Select the "Export" tab, then "start export." Per the information on this page, this will create a file that will consist of "a series of JSON files: one per channel per day with activity, plus a file containing information about all members of your team."

It will take Slack some time to prepare this export, especially if you have a lot of message traffic and media in the channels (it also exports images). After some time, it will e-mail you to let you know the export is ready for download.

  • 1
    Sounds like the question is about another kind of archiving (as in putting the channel away)? – grawity Aug 23 '16 at 22:28
  • In the context of Slack this is what I'd expect a person to mean by "archive." Storing the existing chatlog in that channel away and putting it into cold storage. If you delete the channel later, I suppose that'd be a different operation. – Bradley Evans Aug 23 '16 at 22:31
  • For reference, on individual channels this help page ( get.slack.help/hc/en-us/articles/213185307-Deleting-a-channel ) shows archiving and deleting to be seperate operations, not a single "save and close" operation. – Bradley Evans Aug 23 '16 at 22:32
1

Here is my quick PHP script that i use to clear all sk channels

<?php 
$json_url = "https://slack.com/api/channels.list?token=YOURTOKEN&pretty=1";
$json = file_get_contents($json_url);


$data = json_decode($json);
$m=count($data->channels);
echo "total channels:".$m."<br>";

$k=0;

for($i=0; $i<$m; $i++) {

//echo $data->channels[$i]->name."<br>";
$cn=$data->channels[$i]->name;
$cn2=$data->channels[$i]->is_archived;

if (strpos($cn, 'sk-') !== false and $cn2==0) {
   echo $data->channels[$i]->name."(".$cn2.") (id=";
   $cid=$data->channels[$i]->id;
   echo $cid.');<br>';

$json_url2 = "https://slack.com/api/channels.archive?token=YOURTOKEN&channel=".$cid."&pretty=1";
$json2 = file_get_contents($json_url2);
++$k;
}

}
echo "total sk channels archived:".$k."<br>";

?>
1

Here is a kotlin script I wrote recently to archive all channels that hadn't had a new message in them in 3 months.

Note: If you hit the API limit in the logs, wait 2 minutes and run it again.

import com.squareup.moshi.Moshi
import khttp.get
import java.time.ZonedDateTime

data class JsonChannelsList(
        val channels: List<JsonChannel>
)

data class JsonChannel(
        val id: String,
        val name_normalized: String,
        val is_archived: Boolean,
        val is_private: Boolean
)

data class JsonMessages(
        val messages: List<JsonMessage>
)

data class JsonMessage(
        val username: String,
        val text: String,
        val ts: String
)

data class ArchiveableChannel(
        val id: String,
        val name: String
)

/**
 * Arg 0 is your slack api token with user access (to be able to read channels)
 */
fun main(args: Array<String>) {
    val moshi = Moshi.Builder().build()
    val jsonChannelsAdapter = moshi.adapter(JsonChannelsList::class.java)
    val jsonMessagesAdapter = moshi.adapter(JsonMessages::class.java)

    val slackToken = args[0]

    val list = get("https://slack.com/api/channels.list?token=$slackToken&exclude_archived=true&pretty=1")

    val jsonChannelsList: JsonChannelsList = jsonChannelsAdapter.fromJson(list.jsonObject.toString()) as JsonChannelsList;
    println("${list.statusCode} - ${list.jsonObject}")
    val channels = jsonChannelsList.channels
    val archivable = arrayListOf<ArchiveableChannel>()

    println("Finding Archiveable Channels")
    for (c in channels) {
        println("Channel ${c.name_normalized} ")
        if (c.is_private) {
            println("private, skipped.")
            continue
        }
        if (c.is_archived) {
            println("archived already.")
            continue
        }
        val channelId = c.id;
        val THREE_MONTHS_AGO = ZonedDateTime.now().minusMonths(3).toEpochSecond()
        val msgs = get("https://slack.com/api/channels.history?token=$slackToken&channel=$channelId&oldest=$THREE_MONTHS_AGO&count=1")
//        print("messages ")
//        println(msgs.jsonObject)

        val jsonMessages: JsonMessages = jsonMessagesAdapter.fromJson(msgs.jsonObject.toString()) as JsonMessages
        if (jsonMessages.messages.isEmpty()) {
            archivable.add(ArchiveableChannel(c.id, c.name_normalized))
        }
    }

    println("Archive Channels")
    for (archiveChannel in archivable) {
        println(archiveChannel)
        val archivedResult = get("https://slack.com/api/channels.archive?token=$slackToken&channel=${archiveChannel.id}&pretty=1")
        print("${archivedResult.statusCode} ")
        print("${archiveChannel.name} ")
        print("${archivedResult.jsonObject}.")
    }
    println("done.")
    println("Archived ${archivable.size} channels.")
    println("Archived $archivable")
}

Dependencies needed:

compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.3"
compile 'khttp:khttp:0.1.0'
compile("com.squareup.moshi:moshi-kotlin:1.8.0")

Source project here: https://github.com/novoda/spikes/tree/master/slackdeath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.