Is it possible to delete a snippet in php?

I need to make a One time use page that automatically deletes its self after it is accessed and all of the correct info is inputted. This is for onboarding new clients to their modx installs.

Yes you can delete a snippet.

Something like this should work: (untested)

$snippet = $modx->getObject('modSnippet', $snippet_id);
$snippet->remove();

So why do you want to delete a snippet and not a resource/page?

what would the code look like for deleting a template? Would it be ā€˜modTemplateā€™ or something like that? Also if I have a php file that was uploaded through sftp how would I call a system setting in that php file? Ive tried to use the $modx object but it doesnā€™t work.

Yes, ā€œmodTemplateā€ is the class for the templates.


If you want to have access to $modx, you have to load MODX externally:
https://docs.modx.com/current/en/extending-modx/modx-class/loading-externally

The code is somewhat different if you are using MODX 3.


Also after deleting a snippet you probably have to clear the cache ($modx->cacheManager->refresh();) or the snippet code will still exist in the cache.

1 Like

Thank you!!!

Iā€™m almost certain that you donā€™t really want to delete a snippet. If Iā€™m understanding you, you want a page that the user canā€™t access more than once.

If you can give us some more details, like how the user gets to the page, and what the page is for (registration, for example), and whether multiple users will go through the process, we might be able to suggest a better solution.