Bulk delete of questions / categories

Bulk delete of questions / categories

by James Reed -
Number of replies: 22
Hi,

Apologies if this is a silly question or if I have missed something, but I need a way to delete quite a lot of old questions and categories. I know that I can go into each category and select all the questions before deleting them, but this seems slow and a bit clumsy and takes a long time (especially with our server!).

Is there perhaps some way of directly editing the database to accomplish this more easily?

Thanks in advance

James
Average of ratings: -
In reply to James Reed

Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It is dangerous to go directly into the database and delete the questions, because there are other tables that link to those questions. You would need to understand the full quiz/questionbank db structure http://docs.moodle.org/en/Development:Quiz_database_structure, and delete the associated information before deleting the questions themselves.

Actually, what you case safely do is to set the hidden column to true (1) for all the questions you want to get rid of. That has almost the same effect as deleting the questions completely, but does not break any of the places that refer to them. (This is what Moodle actually does when you delete a question that is in use.) Of course, it leaves all the data in your database.

The other option is to write a PHP script that calls the delete function for all the questions you want gone.
In reply to Tim Hunt

Re: Bulk delete of questions / categories

by Zuidhof Patrick -
i have noticed a much bigger problem. When you delete a course (using moodle 1.7) with a quiz than the qeustions of that course are also gone. And in every other course!!!

So people beware of deleting a course with a quiz!!
You can still delete quiz in a course and keeping the questions but when you still delelete afterwards the whole course than the questions are gone from the central database.

Besides this Moodle 1.7 has many more problems like inserting links, data,documents


In reply to Zuidhof Patrick

Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
When you delete a course, everything belonging to that course, including all its questions, should be deleted.

What exactly is the problem?
In reply to Tim Hunt

Re: Bulk delete of questions / categories

by Zuidhof Patrick -
an example.
You make a quiz with 30 questions. A colleque says hey that's a cool quiz can i use it? So he/she imports your quiz or he/she picks the SAME questions from the central database. All questions are available for every docent or people with a higher profile in moodle.

But later your colleque decided to delete the course and yes everything in that course is gone and also in yours if you used the same questions!!!

Moodle always checks if the questions are used elsewhere but this check dont work so the questions in your original course are also gone and everywhere.

So it's "dangerous" to share courses (witihn the same moodle) with the same questions or quiz if the other to decide deleting his or her course.


In reply to Zuidhof Patrick

Re: Bulk delete of questions / categories

by John Isner -
Hi Patrick,
I think you are describing two different scenarios.

In the first scenario, your colleague imports (or more likely restores from backup) a quiz from your course. This makes a copy of everything (questions, categories). The copies now belong to your colleague, and anything he does with them will not affect your course.

In the second scenario, your colleague picks questions from a "central database." There's no such notion in Moodle, but the closest thing is a database belonging to a course in which all categories are published. Your colleague can very well make a quiz from the questions in such a database, but the questions will be read only in his course. He cannot change them and he cannot delete them.
Average of ratings: Useful (1)
In reply to John Isner

Re: Bulk delete of questions / categories

by Zuidhof Patrick -
Hi John,

Believe me John i've researched and tried it. And of course a docent cannot delete courses. But please be my guest and try the following:

Make a course, make some example questions
Make another course use the the SAME questions or import the whole quiz in it.
It don't make a difference how you do it both scenarios goes wrong (in fact every possible scenario) (using moodle 1.7)
1.6.3 hadn't this failure!!

Than delete a course ,which one it really don't matter, and than try the quiz again , you get the message no questions found!!

When i import a whole course nothing goes wrong when i delete the main course or the copy one except the quiz/questions.

And i've done it with every possible scenario.

In reply to Zuidhof Patrick

Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Can anyone else test and confirm this? I am rather busy at the moment.
In reply to Tim Hunt

Re: Bulk delete of questions / categories

by Dean Thayer -
Yes, the problem occurs just as Mr. Patrick describes. I have just discovered this to be disastrously true in my production Moodle instance. I deleted some old courses and now quizzes are broken all over the place. I am in big trouble.
In reply to Dean Thayer

Re: Bulk delete of questions / categories

by Ulrike Montgomery -

I have just discovered the same problem. We didn't have the problem with 1.6, but ever since our upgrade to 1.8.2 the quiz questions are deleted completely if only 1 course using these questions is deleted, even though 10 other courses use the same questions.

Does anybody have an idea what the problem could be?

-- Ulrike

In reply to Ulrike Montgomery

Vast: Re: Bulk delete of questions / categories

by Olli Salo -
Hi, I too can confirm this. This bug is one of the worst our Mdl 1.6.5+ production site has suffered - ever. We have created one very popular moodle course and copied it via backup/restore to several faculties within the same mdl-server. At the beginnin everything seemed ok, since there were no quiz results submitted by the students. At this time the backup/restore seemed to work correctly. But when we were forced to backup and restore one course with students' quiz attempts in it, that very course AND also the other courses got scrambled: quiz questions got deleted from the already answered quiz sheets submitted by the students, quiz categories disappeared from other teachers' course areas and quizzes lost many of their questions.

Now, since the backup/restore doesn't work as it should, we were forced to comment out the restore feature from <mdl-server-url>/files/files.php and we are in a situation where we are unable to restore any course because of the fear of messing up the db again (I have had to restore our postgresql db now twice because of this bug). The reason for this is exactly the same as what many of you have described: the questions are nowhere to be found when a course with quizzes is deleted. The quiz results are there, but the quiz sheets submitted by the students and the quiz templates lack questions. In many cases the non-published (we don't use the "publish categories") quiz categories are deleted, too.

Please see and vote for the issue I submitted a week ago if you feel you're suffering from the same problem:

http://tracker.moodle.org/browse/MDL-11081

Olli S. / FINLAND
In reply to Olli Salo

Re: Vast: Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm back from leave now. I'll look into this once I get back to work on Monday.
In reply to Tim Hunt

Vast: Re: Vast: Re: Bulk delete of questions / categories

by Olli Salo -
First: thank you Tim for committing the MDL-11081 patch: it seems to work well. However, I'd really appreciate it if you could explain your comment in the MDL-11081 patch, which I have attached below:

+ // TODO: we should also consider other questions that are used by
+ // random questions in this quiz, but that is very hard.


My question is: what kind of problems can we still face with the MDL-11081 patch installed? And more: what kind of solution would fix this quiz problem permanently?

It's really awesome that 1.9 will fix many of these quiz issues. But as we cannot take 1.9 to our production sites yet, I think the only way now is to patch 1.6-1.8 as well as possibly.
In reply to Olli Salo

Re: Vast: Re: Vast: Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In the tracker, do you see the 'Fix version/s' field. This tells you which versions of Moodle I fixed the bug is, and as you can see, in this case it is "1.9, 1.6.6, 1.7.3, 1.8.3".

In addition, if you click on the 'Version Control' link after the description, you can find out exactly how the code changed to fix this issue.


The underlying issue was that there was the function 'quiz_question_list_instances', which is supposed to return a list of all the quizzes that use a particular question, had not been implemented. That is, it always said 'this question is not used by any quizzes'. So when a course was being deleted (either really deleted, or emptied out before resotring a backup into it, Moodle thought it was safe to delete the questions when actually it wasn't.

So the solution that Janne Mikkonen worked out, and I edited slighly and committed, was to return an list of quizzes that was mostly accurate.

Why only mostly accurate? Well there are two ways a quiz can use question X. Either the quiz can include question X directly, or the quiz can include a random question, and then the random question can pick question X of one or more student's attemtpt. At the moment, we are only finding the first sort of usage. That is realy what the comment notes - that we really ought to find examples of the second sort of usage too, but we have not yet because it is too difficult (and it would be very expensive, in performace terms, to try.)

However, I am fairly sure it does not matter, because actually the delete code, which is the only place where this is important, actually makes the delete or keep decision at the category level, not the individualy question level, and as far as I can make out, the way it works means that it will never delete a questions needed by a random question. However, it would be good if someone else can study the code and confirm this. The way to start is to grep for all the places (about 5) where quiz_question_list_instances is referred to.

I should also note that this is no longer a problem in Moodle 1.9, Jamies changes to the question bank mean that questions are no longer shared between courses, so when you delete a course, all its qeustions can safely be deleted too, so life is much simpler, and hopefully more bug-free. Thanks Jamie.
In reply to Tim Hunt

Re: Vast: Re: Bulk delete of questions / categories

by Raza Yousef -

I am having great difficulty deleting unwanted questions from the default category although I deleted all the quizzes I had set up and transfered all the questions to the default category.  I tried creating one quiz with all the questions from the default category and deleted the quiz but the questions still remain.  is there a quick way to do this rather than delete all the 900 questions one by one. Yawn!

Average of ratings: Useful (1)
In reply to Olli Salo

Re: Vast: Re: Bulk delete of questions / categories

by Ghassan Geara -
Hi Olli,

We faced the same problem today while restoring a course (we're using 1.6.5).
We needed to have the questions used for 2 quizzes, but after restore, the quizzes were empty!

Any solution found since sept.07?
Thanks.
In reply to Ghassan Geara

Re: Vast: Re: Bulk delete of questions / categories

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I did fix that bug a couple of weeks ago, but I can't remember how many branches I back-ported it to. The tracker will know.
In reply to James Reed

Re: Bulk delete of questions / categories

by John Isner -
Rahim,
Getting back to your original question: How about creating a new "master" category and making all your existing categories children of the master category. Then delete the master category. I'm pretty sure I have done this in version 1.6.3. It should be easy to make a small experiment first.
In reply to John Isner

Re: Bulk delete of questions / categories

by nabil mohd -

hye,

just want to ask the same question as James Reed..is there any way that i could delete the parent category together with it child and all content in there without deleting them one by one before you could delete the category itself.. 

it take too long too delete one by one and could you imaging if you have more than 1000 question in one category..could anyone help me? may be could create new function that if you delete the parent category, automatically the child and its content will deleted also..

Attachment 1.jpg
Average of ratings: Useful (1)
In reply to nabil mohd

Re: Bulk delete of questions / categories

by George S. -

After much searching and frustration I found a way to 'easily' bulk delete categories filled with hundereds of sub-categories and thousands of questions which involves using the fact that each moodle quiz has a default category for questions. You can only see this default category when you click on Question Bank > Categories from 'within' the quiz. 

To delete large numbers of questions/categories,

1. Create a new quiz.

2. From 'within' that quiz click on Question Bank > Categories.

3. Move the category or categories you would like to delete into the category "Default for <quiz name>". Make sure that they are actually 'inside' this category (i.e., the category that you would like to delete is contained within the category "Default for <quiz name>"; it should be graphically directly below this category in the GUI, and also indented to the right of it).

4. Go back to the course and delete the quiz.

The questions are 'magically' gone, as far as you the teacher are concerned. At least some are likely to still be in the database somewhere, since questions that have been used are 'just' hidden from sight. But from a teacher's perspective, they're gone.

In reply to George S.

Re: Bulk delete of questions / categories

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

They do not actually get deleted from the database.  Another way to do this is create a category called Questions to Delete.  Then start deleting all the categories with the questions that you wanted deleted.  It will prompt to move the questions when you delete the category - move them all to Questions to Delete.  At that point you can then go to that category, select all and delete.

As Tim mentions, it is not easy to delete from the database but it is possible.  It is easier if they have never been used in a quiz (and realistically, if they have been used in a quiz, you should not be deleting them anyway....) - there are just a few tables that you need run scripts on using the quiz id in your search terms.

In reply to Emma Richardson

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.