{"id":252,"date":"2022-09-21T13:43:42","date_gmt":"2022-09-21T11:43:42","guid":{"rendered":"https:\/\/blog.bardalen.no\/?p=252"},"modified":"2022-09-26T21:05:19","modified_gmt":"2022-09-26T19:05:19","slug":"delete-specific-files-in-sharepoint","status":"publish","type":"post","link":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/","title":{"rendered":"Delete specific files in SharePoint"},"content":{"rendered":"\n<p>Have you ever been in a situation where you need to delete files of a certain type, or file names containing some key words? Well, I have recently.<\/p>\n\n\n\n<p>After a &#8211; in general &#8211; successfull migration from on-prem file server to SharePoint we noticed that we also copied over a bunch of temporary files (*.tmp) and files beginning with the characters ~$. This is temporary files and garbage from Word and Excel that&#8217;s been accumulated over the years and not something we need to store. How on earth are we going to clean up this mess?<\/p>\n\n\n\n<p>As always, PowerShell to the rescue! Make sure you are up to date on your PS Module called <strong>PnP.PowerShell<\/strong> by running this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Update-Module -Name PnP.PowerShell<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t have it already, run this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Install-Module -Name PnP.PowerShell<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Warning:<\/p><cite>In version 1.11.0 of  PnP PowerShell there is a bug that breaks this method. Try install the very latest release with this command:<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>Install-Module -Name PnP.PowerShell -AllowClobber -AllowPrerelease -SkipPublisherCheck<\/code><\/pre>\n\n\n\n<p>Next we need to give this module permission to do stuff in our tenant. To trigger a consent prompt, we use this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Register-PnPManagementShellAccess<\/code><\/pre>\n\n\n\n<p>Log in with a user with appropriate&nbsp;rights to be able to consent applications in Azure AD.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The script<\/h2>\n\n\n\n<p>Below is the complete script you can run to get rid of all files of a spesific type in a SharePoint document library. Take note of the variables you need to adjust to fit your need.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$SiteURL = \"https:\/\/customer.sharepoint.com\/sites\/AllTheFiles\"\n$LibraryName = \"Shared Documents\"\n$SearchString = \"*.tmp\"\n\n#Connect to SharePoint Online\nConnect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)\n\n#Create a list of files that matches the search string\n$Files = Find-PnPFile -List $LibraryName -Match $SearchString\n\nForEach ($File in $Files)\n{\n    Write-Host (\"Deleting file: '{0}' at '{1}'\" -f $File.Name, $File.ServerRelativeURL)\n    \n    #Deletes the item\n    Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle\n        \n}\n\n#Optional: Empty the recycle bin. Comment out or not use this if you want to let the files remain in recycle bin.\n\nGet-PnPRecycleBinItem | Where-Object LeafName -like $SearchString | Clear-PnpRecycleBinItem -Force<\/code><\/pre>\n\n\n\n<p><strong>Sources:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pnp.github.io\/powershell\/\">PnP PowerShell | PnP PowerShell<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been in a situation where you need to delete files of a certain type, or file names containing some key words? Well, I have recently. After a &#8211; in general &#8211; successfull migration from on-prem file server to SharePoint we noticed that we also copied over a bunch of temporary files (*.tmp) and files beginning with the characters ~$. This is temporary files and garbage from Word and Excel that&#8217;s been accumulated over the years and not&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\">Les mer<span class=\"screen-reader-text\"> Les mer<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,64,55],"tags":[62,63,22],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-microsoft-365","category-powershell","category-sharepoint","tag-files","tag-search","tag-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Delete specific files in SharePoint - Blog.bardalen.no<\/title>\n<meta name=\"description\" content=\"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\" \/>\n<meta property=\"og:locale\" content=\"nb_NO\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Delete specific files in SharePoint - Blog.bardalen.no\" \/>\n<meta property=\"og:description\" content=\"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog.bardalen.no\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-21T11:43:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-26T19:05:19+00:00\" \/>\n<meta name=\"author\" content=\"Tony Bardalen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Skrevet av\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tony Bardalen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Ansl. lesetid\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutter\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\"},\"author\":{\"name\":\"Tony Bardalen\",\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a\"},\"headline\":\"Delete specific files in SharePoint\",\"datePublished\":\"2022-09-21T11:43:42+00:00\",\"dateModified\":\"2022-09-26T19:05:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\"},\"wordCount\":239,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a\"},\"keywords\":[\"Files\",\"Search\",\"SharePoint\"],\"articleSection\":[\"Microsoft 365\",\"PowerShell\",\"SharePoint\"],\"inLanguage\":\"nb-NO\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\",\"url\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\",\"name\":\"Delete specific files in SharePoint - Blog.bardalen.no\",\"isPartOf\":{\"@id\":\"https:\/\/blog.bardalen.no\/#website\"},\"datePublished\":\"2022-09-21T11:43:42+00:00\",\"dateModified\":\"2022-09-26T19:05:19+00:00\",\"description\":\"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#breadcrumb\"},\"inLanguage\":\"nb-NO\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\/\/blog.bardalen.no\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Delete specific files in SharePoint\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.bardalen.no\/#website\",\"url\":\"https:\/\/blog.bardalen.no\/\",\"name\":\"Blog.bardalen.no\",\"description\":\"A blog about cloudy stuff\",\"publisher\":{\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.bardalen.no\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"nb-NO\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a\",\"name\":\"Tony Bardalen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"nb-NO\",\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/433273fdcc418ba1a5101686ae0fa85fc15baf53a2bafc63eb84bce1911caa0e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/433273fdcc418ba1a5101686ae0fa85fc15baf53a2bafc63eb84bce1911caa0e?s=96&d=mm&r=g\",\"caption\":\"Tony Bardalen\"},\"logo\":{\"@id\":\"https:\/\/blog.bardalen.no\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Delete specific files in SharePoint - Blog.bardalen.no","description":"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/","og_locale":"nb_NO","og_type":"article","og_title":"Delete specific files in SharePoint - Blog.bardalen.no","og_description":"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.","og_url":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/","og_site_name":"Blog.bardalen.no","article_published_time":"2022-09-21T11:43:42+00:00","article_modified_time":"2022-09-26T19:05:19+00:00","author":"Tony Bardalen","twitter_card":"summary_large_image","twitter_misc":{"Skrevet av":"Tony Bardalen","Ansl. lesetid":"2 minutter"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#article","isPartOf":{"@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/"},"author":{"name":"Tony Bardalen","@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a"},"headline":"Delete specific files in SharePoint","datePublished":"2022-09-21T11:43:42+00:00","dateModified":"2022-09-26T19:05:19+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/"},"wordCount":239,"commentCount":1,"publisher":{"@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a"},"keywords":["Files","Search","SharePoint"],"articleSection":["Microsoft 365","PowerShell","SharePoint"],"inLanguage":"nb-NO","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/","url":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/","name":"Delete specific files in SharePoint - Blog.bardalen.no","isPartOf":{"@id":"https:\/\/blog.bardalen.no\/#website"},"datePublished":"2022-09-21T11:43:42+00:00","dateModified":"2022-09-26T19:05:19+00:00","description":"Delete specific files in SharePoint document libraries. Search for file types- or names to get rid of single files.","breadcrumb":{"@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#breadcrumb"},"inLanguage":"nb-NO","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.bardalen.no\/index.php\/2022\/09\/21\/delete-specific-files-in-sharepoint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/blog.bardalen.no\/"},{"@type":"ListItem","position":2,"name":"Delete specific files in SharePoint"}]},{"@type":"WebSite","@id":"https:\/\/blog.bardalen.no\/#website","url":"https:\/\/blog.bardalen.no\/","name":"Blog.bardalen.no","description":"A blog about cloudy stuff","publisher":{"@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.bardalen.no\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"nb-NO"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/824afefe68762a6905723a53c0f28f9a","name":"Tony Bardalen","image":{"@type":"ImageObject","inLanguage":"nb-NO","@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/433273fdcc418ba1a5101686ae0fa85fc15baf53a2bafc63eb84bce1911caa0e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/433273fdcc418ba1a5101686ae0fa85fc15baf53a2bafc63eb84bce1911caa0e?s=96&d=mm&r=g","caption":"Tony Bardalen"},"logo":{"@id":"https:\/\/blog.bardalen.no\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":6,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":263,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions\/263"}],"wp:attachment":[{"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bardalen.no\/index.php\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}