I need to delete records in a survey before they reach the big 5K threshold. The problem is, I need to delete MOST of the records, but not ALL of them. Since it's a survey, I can't seem to open it in a datasheet view as I can in a regular list. I'd also rather not go through each record and delete one by one (which would take forever). Ideas?
2 Answers
You can use "Content and Structure" feature to delete multiple surveys. Please remember to uncheck the surveys that you don't want to delete.
Delete all data from an existing survey
One option is to create a view for the survey that will show all the items you want deleted. Then run a script that will delete all items in a list view. Check out this script for it SP.List.recycleItems.js
;(function(){
var recycleItems = function(listViewName){
listViewName = listViewName || false;
var successMessage = 'Item(s) Recycled',
ctx = this.get_context(),
list = this,
items,
views = this.get_views(),
view,
counter,
noop = function(){},
error = function(sender, args){
var message = args.get_message() + '\n' + args.get_errorCode();
console.error(message);
},
execute = function(payload,callback){
payload = payload || null;
callback = callback || noop;
if(typeof(payload) === 'function'){
callback = payload;
}
if(payload !== null && callback !== payload){
ctx.load(payload);
}
ctx.executeQueryAsync(callback.bind(null,payload),error);
},
getView = function(){
view = views.getByTitle(listViewName);
execute(view,getItems);
},
getItems = function(){
var camlQuery;
if(!listViewName){
camlQuery = new SP.CamlQuery.createAllItemsQuery();
}
else{
camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(view.get_listViewXml());
}
items = list.getItems(camlQuery);
execute(items,recycleItems);
},
success = function(sender, args){
console.log(counter + ' ' + successMessage);
},
recycleItems = function(){
var enumerator = items.getEnumerator();
var recycleitems = [];
while(enumerator.moveNext()){
var item = enumerator.get_current();
recycleitems.push(item);
}
for(var i = 0; i < recycleitems.length; i++){
recycleitems[i].recycle();
counter = i+1;
}
execute(success);
};
if(!listViewName){
console.error('Error: You must provide a View Name');
}
else{
getView();
}
}
if(_v_dictSod['sp.js'].state === Sods.loaded){
window.SP.List.prototype.recycleItems = recycleItems;
}
else{
RegisterSod("sp.res.resx", "/_layouts/15/ScriptResx.ashx");
RegisterSod("sp.js", "/_layouts/15/sp.debug.js");
RegisterSodDep("sp.js", "sp.res.resx");
SP.SOD.executeFunc('sp.js',false,function(){
window.SP.List.prototype.recycleItems = recycleItems;
});
}
}())
USAGE:
var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('<List Title>');
ctx.load(list);
ctx.executeQueryAsync(
function(){
list.recycleItems('<Name of List View');
},
function(sender,args){
console.error(args.get_message() + '\n' + args.get_errorCode();
}
);
The above code sends items to the recycle bin where they would need to be deleted to restore space on the site collection. You can use SP.List.deleteItems.js to skip the recycle bin and delete completely in one swift keystroke.