Delete programmatically all pages in a project by page type

Vote:
 

We have a job that creates pages programmatically, resulting in thousands of pages of a specific type.  There is an issue, and we need to clear these pages, but the CMS UI/Admin isn't able to handle it.  Instead, I have been trying to delete these pages programmatically.  It removes them visually in the CMS content editor, but they still show in Admin Manage Content. 

What am I missing?  I've tried many variations, but here is my latest code:

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentType = contentTypeRepository.Load<MyTestPage>();
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentList = ServiceLocator.Current.GetInstance<ContentStore>().ListContentOfContentType(contentType, false).ToList();

foreach (var course in contentList)
{
    var page = contentRepository.Get<MyTestPage>(course.ContentLink);
    var pageClone = page.CreateWritableClone() as MyTestPage;
    contentRepository.Delete(clonePage.ContentLink, true, AccessLevel.NoAccess);
}

Any help would be greatly appreciated!

#284616
Edited, Jul 29, 2022 15:08
Vote:
 

Hi Kevin,

You don't need to CreateWriteableClone with using Delete(). Just pass the contentlink of the page in.

After you delete the page using your code, what appears when you click the content page inside 'Manage Content' ? What appears in the right hand pane?

#284696
Jul 31, 2022 12:30
Vote:
 

Hi Surjit, thanks for the reply. 

The CreateWritableClone didn't make sense to me, but I saw a post somewhere that suggested it was needed.  Either way produced the same result.

In Manage Content, clicking on these pages produces mixed results.  Sometimes it simply selects it as expected, ready to use the action buttons.  Other times, it it throws a 404-Not Found.  I have not found a pattern as to why/when this occurs.  A browser refresh seems temporarily fix the 404.

Before the job is run, I can manually delete any of these pages.  After it is run, in Manage Content I can also manually delete a page (when no 404).

Thanks in advance for any ideas!

#284750
Aug 01, 2022 12:07
Vote:
 

Hi Kevin,

Did you inspect the logs? On local they are in the App_Data folder. There should be an indication if the page delete was not executed successfully. 

Did you run your code from within a scheduled job? Try deleting a page programmatically from within a controller instead and see if it produces the same result.

An event is triggered when content is being deleted, so you can also hook into this event to get some more details. 

[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class ContentDeleteInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var events = context.Locate.ContentEvents();
        events.DeletedContent += EventsOnDeletedContent;
    }

    private void EventsOnDeletedContent(object sender, DeleteContentEventArgs e)
    {
        //check 'e' for details about the deleted content
        throw new NotImplementedException();
    }
}

Hope this helps 😄

#284857
Edited, Aug 03, 2022 1:01
Kevin Gainey - Aug 05, 2022 17:17
Hi Ynze, thanks for the reply. I have inspected the logs, and no errors are thrown. I'll have to check out your other suggestions. -Kevin
Vote:
 

Hello Kevin, This could be a piece of your scheduled job - 

var allPages = ContentLoader.GetChildren<WebPage>();
OnOutputResultChanged($"Found {allPages.Count()} pages");
OnStatusChanged($"Found {allPages.Count()} pages");

foreach (var page in allPages)
{
	try
	{
		IContentRepositoryExtension.Delete(_contentRepository, page.ContentLink, true);
	}
	catch (Exception ex)
	{
		OnOutputResultChanged($"Delete page failed {page.DisplayName} msg: {ex.Message}");
	}
}
#284948
Edited, Aug 04, 2022 14:58
Praful Jangid - Aug 05, 2022 16:26
Well, good choise of using IContentRepositoryExtension.Delete(), this will do your house cleaning stuff, instead of just deleting the content.
Vote:
 

Thanks Manoj.  I was hopeful this different approach would work for me, especially after Praful's comment.  Unfortunately, it produced the same results as the other things I've tried.

On a side note, I wasn't able to use the ContentLoader.GetChildren method in my app.  This is the error I was presented with:

So I just used my original code to derive the page instead.  That shouldn't make a difference though.

#284994
Aug 05, 2022 17:08
Vincent - Aug 14, 2022 23:28
The warning seems you're referencing the class directly instead of interface. You'll need to switch to IContentLoader (it won't fix your current issue, but concrete internal class should be avoided to use directly)
Vote:
 

Try this code, I have tested following code. Please let me know if that work

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();

var contentType = contentTypeRepository.Load<ProductPage>();
var usages = contentModelUsage.ListContentOfContentType(contentType);

foreach (var contentUses in usages)
{
    try
    {
        contentRepository.Delete(contentUses.ContentLink, true);
    }
    catch (Exception ex)
    {
        return $"Delete page failed {contentUses.Name} msg: {ex.Message}";
    }
}
#285226
Aug 09, 2022 5:21
Vote:
 

Thank you everyone for your help.  Unfortunately none of the solutions presented got me past my issue.  I suspect the pages I am trying to delete were improperly constructed (programmatically by another job a consultant implemented, but not here for that), and are in conflict with others, preventing deletion.  

Again, lots of good information!  Thanks, but I guess I'll have to close the issue, and call it unresolved.

#285492
Aug 13, 2022 17:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.