8

How to delete a view programatically?

It's crazy I can't find proper answer for such a simple question.

5

Views has a function in it called views_delete_view, it takes a view object as a param. Try views_delete_view($yourloaded_view), that should do the trick.

13

Give a try to the following code to delete the view:

// Check the view id the one that you are looking for.
if ($view = views_get_view($name)) {
  // Delete the view if it is the one that you are looking for.
  views_delete_view($view);
}
  • Thanks. Anyone can explain why I have to use this syntax: $view->delete(); ? Above it there is views_delete_view($view) function already, which (as @Jance said) should do the trick. – David Sep 23 '14 at 18:26
  • 1
    You don't. Just use the first two lines, up to views_delete_view. This answer is way more elaborate than necessary: views_delete_view calls $view->delete(); and then the delete method does the cache invalidation. – user49 Sep 23 '14 at 18:51
  • @chx Thanks for correcting. I have updated the answer. @David Please ignore the $view->delete(); , if required you may check the answer now. – Ankit Agrawal Sep 23 '14 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.