Unrelated documents visible under asset pane "For this block"

This article describes an issue when a new block is created there are unrelated documents that appear under the asset pane in the "For this block" section.

A similar issue was found in an upgraded site from 7.1 to latest Episerver. Product Development investigated and found that somehow there was some content under the contentRootFolder that did not belong. It could have happened during migration of vpp files or Blobs.

The solution was to move those content from ContentRootFolder to another parent like "For all sites" by using following code (just an example). This is a sample and is provided as is for educational purposes. The recommendation prior to running any APIs or scripts is backup site files and database.

            var repository = ServiceLocator.Current.GetInstance();
            // Get all media Items from the ContentAssetsRoot

            var itemsToMove = repository.GetChildren(SiteDefinition.Current.ContentAssetsRoot);
            foreach (var item in itemsToMove)
                var readOnlyContent = item as IReadOnly;
                var content = item;
                if (readOnlyContent != null && readOnlyContent.IsReadOnly)
                    content = (IContentMedia)readOnlyContent.CreateWritableClone();

                //Set the new parent
                content.ParentLink = SiteDefinition.Current.GlobalAssetsRoot;
                repository.Save(content, SaveAction.ForceCurrentVersion | SaveAction.Save, AccessLevel.Read);