Delete Older Messages from a Yammer Group

%3CLINGO-SUB%20id%3D%22lingo-sub-140969%22%20slang%3D%22en-US%22%3EDelete%20Older%20Messages%20from%20a%20Yammer%20Group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-140969%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20you%26nbsp%3Bneed%20to%20delete%20older%20messages%20from%20a%20particular%20group%2C%20there's%20no%20built-in%20way%20to%20do%20that.%26nbsp%3B%20However%2C%20like%20most%20things%2C%20you%20can%20use%20the%20REST%20API%20as%20well%20as%20PowerShell%20to%20accomplish%20this.%26nbsp%3B%20Two%20uses%20I've%20seen%20for%20this%20functionality%20is%20to%20meet%20maximum%20retention%20policies%20(e.g.%20we%20need%20to%20remove%20all%20content%20older%20than%2012%20months)%2C%20as%20well%20as%20some%20groups%20are%20so%20prolific%20that%20old%20information%20clogs%20search%20results.%26nbsp%3B%20Whatever%20your%20need%2C%20here's%20the%20script%20to%20accomplish%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20like%20all%20the%20other%20scripts%20I've%20supplied%2C%20you%20need%20to%20get%20a%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FYammer-Developer%2FGenerating-an-Administrator-Token%2Fm-p%2F97058%22%20target%3D%22_blank%22%3Epermanent%20token%3C%2FA%3E.%26nbsp%3B%20%26nbsp%3BOnce%20you%20have%20that%2C%20copy%20this%20script%20and%20save%20it%20to%20Notepad%2C%20and%20give%20it%20a%20name%20ending%20in%20.ps1%20(rather%20than%20.txt).%26nbsp%3B%20Edit%20the%20file%20to%20have%20the%20correct%20token%20as%20well%20as%20directories%2C%20date%20of%20last%20wanted%20message%2C%20and%20group%20ID.%26nbsp%3B%20Execute%20the%20script%20by%20type%20cmd%20(enter)%2C%20then%20powershell%20(enter)%2C%20then%20.%5C%3CTHE%20name%3D%22%22%20of%3D%22%22%20your%3D%22%22%20file%3D%22%22%3E.%3C%2FTHE%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%23This%20section%20is%20for%20you%20to%20customize%20your%20script%20for%20your%20environment%0A%23If%20the%20directories%20don't%20exist%20or%20the%20token%20is%20wrong%2C%20this%20won't%20work.%0A%0A%23This%20has%20to%20be%20YOUR%20token%0A%20%24token%20%3D%20%22Token%22%20%20%0A%0A%23This%20is%20the%20directory%20where%20your%20messages.csv%20file%20is%20stored%20(from%20export)%0A%20%24SourceDir%20%3D%20%22C%3A%5CData%5CDeleteOld%22%0A%0A%23This%20is%20the%20directory%20where%20the%20csv%20file%20is%20going%20to%20be%20created%0A%20%24OutputDir%20%3D%20%22C%3A%5CData%5CDeleteOld%5COutput%22%0A%0A%23Update%20the%20date%20here%20to%20determine%20what%20is%20the%20date%20at%20which%20it%20will%20start%20deleting%20messages%0A%24TheEnd%20%3D%20(Get-Date%202017-07-01).toString(%22yyyy-MM-dd%22)%0A%0A%23Set%20the%20target%20group%0A%24groupid%20%3D%20123456789%0A%0A%23%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23This%20section%20you%20shouldn't%20need%20to%20modify%0A%20%24Headers%20%3D%20%40%7B%20%0A%20%22Accept%22%20%3D%20%22*%2F*%22%20%0A%20%22Authorization%22%20%3D%20%22Bearer%20%22%2B%24token%20%0A%20%22accept-encoding%22%20%3D%20%22gzip%22%20%0A%20%22content-type%22%3D%22application%2Fjson%22%20%0A%20%22content-length%22%20%3D%20%222%22%20%0A%20%7D%20%0A%20%3CBR%20%2F%3E%23import%20Message%20ID's%20that%20need%20evaluated.%20%0A%0Aimport-csv%20%24SourceDir%5CMessages.csv%20%7C%20where%20%7B%24_.created_at%20-le%20%24TheEnd%20-and%20%24_.group_id%20-eq%20%24groupid%7D%20%7CSelect%20id%2C%20created_at%2C%20group_id%20%7C%20export-csv%20-path%20%24OutputDir%5COutput.csv%0A%20%24messages%20%3D%20import-csv%20%24OutputDir%5COutput.csv%0A%20%24messages%20%3D%20%24messages.id%20%0A%0A%23loop%20through%20all%20Message%20ID's%20%0Aforeach%20(%24message%20in%20%24messages)%7B%20%0A%0A%24uri%20%3D%20%22https%3A%2F%2Fwww.yammer.com%2Fapi%2Fv1%2Fmessages%2F%24message%22%20%0A%20%0A(Invoke-WebRequest%20-Uri%20%24uri%20-Method%20Delete%20-Headers%20%24Headers).content%20%20%20%20%0A%7D%0A%3C%2FPRE%3E%0A%3CP%3EPlease%20post%20below%20if%20you%20find%20the%20script%20useful.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-140969%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAPI%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EREST%20API%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EYammer%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1445760%22%20slang%3D%22en-US%22%3ERe%3A%20Delete%20Older%20Messages%20from%20a%20Yammer%20Group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1445760%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F55%22%20target%3D%22_blank%22%3E%40Tom%20Kretzmer%3C%2FA%3Ethanks%20you%20for%20your%20post!%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20question%3A%20How%20do%20I%20get%20the%20message.csv%20file%3F%20I%20understand%20that%20I%20should%20run%20another%20script%20to%20get%20all%20the%20messages%20from%20the%20group%20from%20which%20I%20want%20to%20delete%20the%20messages.%20Do%20you%20have%20that%20script%3F%20Thank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular Contributor

If you need to delete older messages from a particular group, there's no built-in way to do that.  However, like most things, you can use the REST API as well as PowerShell to accomplish this.  Two uses I've seen for this functionality is to meet maximum retention policies (e.g. we need to remove all content older than 12 months), as well as some groups are so prolific that old information clogs search results.  Whatever your need, here's the script to accomplish this.

 

First, like all the other scripts I've supplied, you need to get a permanent token.   Once you have that, copy this script and save it to Notepad, and give it a name ending in .ps1 (rather than .txt).  Edit the file to have the correct token as well as directories, date of last wanted message, and group ID.  Execute the script by type cmd (enter), then powershell (enter), then .\<the name of your file>.

 

#This section is for you to customize your script for your environment
#If the directories don't exist or the token is wrong, this won't work.

#This has to be YOUR token
 $token = "Token"  

#This is the directory where your messages.csv file is stored (from export)
 $SourceDir = "C:\Data\DeleteOld"

#This is the directory where the csv file is going to be created
 $OutputDir = "C:\Data\DeleteOld\Output"

#Update the date here to determine what is the date at which it will start deleting messages
$TheEnd = (Get-Date 2017-07-01).toString("yyyy-MM-dd")

#Set the target group
$groupid = 123456789

#==================================
#This section you shouldn't need to modify
 $Headers = @{ 
 "Accept" = "*/*" 
 "Authorization" = "Bearer "+$token 
 "accept-encoding" = "gzip" 
 "content-type"="application/json" 
 "content-length" = "2" 
 } 
 
#import Message ID's that need evaluated. import-csv $SourceDir\Messages.csv | where {$_.created_at -le $TheEnd -and $_.group_id -eq $groupid} |Select id, created_at, group_id | export-csv -path $OutputDir\Output.csv $messages = import-csv $OutputDir\Output.csv $messages = $messages.id #loop through all Message ID's foreach ($message in $messages){ $uri = "https://www.yammer.com/api/v1/messages/$message" (Invoke-WebRequest -Uri $uri -Method Delete -Headers $Headers).content }

Please post below if you find the script useful.

2 Replies

@Tom Kretzmerthanks you for your post! 

I have a question: How do I get the message.csv file? I understand that I should run another script to get all the messages from the group from which I want to delete the messages. Do you have that script? Thank you!

Hi @Tom Kretzmer 

 

Thanks for your post, but I would like to know how I make the messages.csv file please.

 

Very thanks in advance for your answer.